diff options
139 files changed, 23665 insertions, 6077 deletions
@@ -10,3 +10,4 @@ src/relooper.js.raw.js src/relooper/*.o src/relooper/*.out +tests/fake/
\ No newline at end of file @@ -48,4 +48,10 @@ a license to everyone to use it as detailed in LICENSE.) * Jasper St. Pierre <jstpierre@mecheye.net> * Manuel Schölling <manuel.schoelling@gmx.de> * Bruce Mitchener, Jr. <bruce.mitchener@gmail.com> +* Michael Bishop <mbtyke@gmail.com> +* Roger Braun <roger@rogerbraun.net> +* Vladimir Vukicevic <vladimir@pobox.com> (copyright owned by Mozilla Foundation) +* Lorant Pinter <lorant.pinter@prezi.com> +* Tobias Doerffel <tobias.doerffel@gmail.com> * Martin von Gagern <martin@von-gagern.net> + @@ -11,6 +11,8 @@ import os, subprocess, sys from tools import shared DEBUG = os.environ.get('EMCC_DEBUG') +if DEBUG == "0": + DEBUG = None newargs = [shared.LLVM_AR] + sys.argv[1:] @@ -1,4 +1,5 @@ #!/usr/bin/env python2 +# -*- Mode: python -*- ''' emcc - compiler helper script @@ -74,7 +75,7 @@ emcc can be influenced by a few environment variables: EMMAKEN_COMPILER - The compiler to be used, if you don't want the default clang. ''' -import os, sys, shutil, tempfile, subprocess, shlex, time +import os, sys, shutil, tempfile, subprocess, shlex, time, re from subprocess import PIPE, STDOUT from tools import shared from tools.shared import Compression, execute, suffix, unsuffixed, unsuffixed_basename @@ -90,7 +91,10 @@ LLVM_OPT_LEVEL = { 3: 3, } -DEBUG = int(os.environ.get('EMCC_DEBUG') or 0) +DEBUG = os.environ.get('EMCC_DEBUG') +if DEBUG == "0": + DEBUG = None + TEMP_DIR = os.environ.get('EMCC_TEMP_DIR') LEAVE_INPUTS_RAW = os.environ.get('EMCC_LEAVE_INPUTS_RAW') # Do not compile .ll files into .bc, just compile them with emscripten directly # Not recommended, this is mainly for the test runner, or if you have some other @@ -118,12 +122,43 @@ if len(sys.argv) == 1: print 'emcc: no input files' exit(1) +# read response files very early on +response_file = True +while response_file: + response_file = None + for index in range(1, len(sys.argv)): + if sys.argv[index][0] == '@': + # found one, loop again next time + print >>sys.stderr, 'emcc: using response file: %s' % response_file + response_file = sys.argv[index][1:] + if not os.path.exists(response_file): + print >>sys.stderr, 'emcc: error: Response file not found: %s' % response_file + exit(1) + + response_fd = open(response_file, 'r') + extra_args = shlex.split(response_fd.read()) + response_fd.close() + + # slice in extra_args in place of the response file arg + sys.argv[index:index+1] = extra_args + #if DEBUG: print >>sys.stderr, "Expanded response file: " + " | ".join(sys.argv) + break + if sys.argv[1] == '--version': - print '''emcc (Emscripten GCC-like replacement) 2.0 -Copyright (C) 2012 the Emscripten authors. + revision = '(unknown revision)' + here = os.getcwd() + os.chdir(shared.path_from_root()) + try: + revision = execute(['git', 'show'], stdout=PIPE, stderr=PIPE)[0].split('\n')[0] + except: + pass + finally: + os.chdir(here) + print '''emcc (Emscripten GCC-like replacement) %s (%s) +Copyright (C) 2013 the Emscripten authors (see AUTHORS.txt) This is free and open source software under the MIT license. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - ''' + ''' % (shared.EMSCRIPTEN_VERSION, revision) exit(0) elif sys.argv[1] == '--help': this = os.path.basename('em++' if os.environ.get('EMMAKEN_CXX') else 'emcc') @@ -146,16 +181,23 @@ Options that are modified or new in %s include: tools/shared.py and also src/settings.js.) Note: Optimizations are only done when compiling to JavaScript, not to intermediate - bitcode. + bitcode, *unless* you build with + EMCC_OPTIMIZE_NORMALLY=1 (not recommended + unless you know what you are doing!) -O2 As -O1, plus the relooper (loop recreation), - plus closure compiler advanced opts, plus - LLVM -O2 optimizations - Warning: Compiling with this takes a long time! + plus LLVM -O2 optimizations -O3 As -O2, plus dangerous optimizations that may - break the generated code! This is not - recommended at all, see the wiki for more - details (you can try -O2 and then add - dangerous optimizations one by one). + break the generated code! This adds + + -s DOUBLE_MODE=0 + -s PRECISE_I64_MATH=0 + --closure 1 + --llvm-lto 1 + + This is not recommended at all. A better idea + is to try each of these separately on top of + -O2 to see what works. See the wiki for more + information. -s OPTION=VALUE JavaScript code generation option passed into the emscripten compiler. For the @@ -177,6 +219,12 @@ Options that are modified or new in %s include: the last compilation phase from bitcode to JavaScript, or else we will remove it by default in -O1 and above. + In -O0, line numbers wil be shown in the + generated code. In -O1 and above, the optimizer + removes those comments. This flag does however + have the effect of disabling anything that + causes name mangling or minification (closure + or the registerize pass). --typed-arrays <mode> 0: No typed arrays 1: Parallel typed arrays @@ -187,14 +235,23 @@ Options that are modified or new in %s include: 2: -O2 LLVM optimizations 3: -O3 LLVM optimizations (default in -O2+) - --llvm-lto <level> 0: No LLVM LTO (default in -O0) - 1: LLVM LTO (default in -O1+) + --llvm-lto <level> 0: No LLVM LTO (default in -O2 and below) + 1: LLVM LTO (default in -O3) Note: If LLVM optimizations are not run (see --llvm-opts), setting this to 1 has no effect. - --closure <on> 0: No closure compiler (default in -O0, -O1) - 1: Run closure compiler (default in -O2, -O3) + --closure <on> 0: No closure compiler (default in -O2 and below) + 1: Run closure compiler. This greatly reduces + code size and may in some cases increase + runtime speed (although the opposite can also + occur). Note that it takes time to run, and + may require some changes to the code. This + is run by default in -O3. + + Note: If closure compiler hits an out-of-memory, + try adjusting JAVA_HEAP_SIZE in the environment + (for example, to 4096m for 4GB). --js-transform <cmd> <cmd> will be called on the generated code before it is optimized. This lets you modify @@ -345,6 +402,41 @@ Options that are modified or new in %s include: for a later incremental build (where you also enable it) to be sped up. + Caching works separately on 4 parts of compilation: + 'pre' which is types and global variables; that + information is then fed into 'funcs' which are + the functions (which we parallelize), and then + 'post' which adds final information based on + the functions (e.g., do we need long64 support + code). Finally, 'jsfuncs' are JavaScript-level + optimizations. Each of the 4 parts can be cached + separately, but note that they can affect each + other: If you recompile a single C++ file that + changes a global variable - e.g., adds, removes + or modifies a global variable, say by adding + a printf or by adding a compile-time timestamp, + then 'pre' cannot be loaded from the cache. And + since 'pre's output is sent to 'funcs' and 'post', + they will get invalidated as well, and only + 'jsfuncs' will be cached. So avoid modifying + globals to let caching work fully. + + To work around the problem mentioned in the + previous paragraph, you can use + + emscripten_jcache_printf + + when adding debug printfs to your code. That + function is specially preprocessed so that it + does not create a constant string global for + its first argument. See emscripten.h for more + details. Note in particular that you need to + already have a call to that function in your + code *before* you add one and do an incremental + build, so that adding an external reference + (also a global property) does not invalidate + everything. + --clear-cache Manually clears the cache of compiled emscripten system libraries (libc++, libc++abi, libc). This is normally @@ -600,7 +692,8 @@ try: ignore_dynamic_linking = False shell_path = shared.path_from_root('src', 'shell.html') js_libraries = [] - keep_debug = False + keep_llvm_debug = False + keep_js_debug = False bind = False jcache = False if use_cxx: @@ -613,18 +706,21 @@ try: absolute_warning_shown = False + settings_changes = [] + for i in range(len(newargs)): newargs[i] = newargs[i].strip() # On Windows Vista (and possibly others), excessive spaces in the command line leak into the items in this array, so trim e.g. 'foo.cpp ' -> 'foo.cpp' if newargs[i].startswith('-O'): - requested_level = newargs[i][2] + # Let -O default to -O2, which is what gcc does. + requested_level = newargs[i][2:] or '2' if requested_level == 's': - print >> sys.stderr, 'emcc: warning: -Os is ignored (use -O0, -O1, -O2)' - else: - try: - opt_level = int(requested_level) - assert 0 <= opt_level <= 3 - except: - raise Exception('Invalid optimization level: ' + newargs[i]) + requested_level = 2 + settings_changes.append('INLINING_LIMIT=50') + try: + opt_level = int(requested_level) + assert 0 <= opt_level <= 3 + except: + raise Exception('Invalid optimization level: ' + newargs[i]) newargs[i] = '' elif newargs[i].startswith('--llvm-opts'): check_bad_eq(newargs[i]) @@ -667,7 +763,8 @@ try: newargs[i] = '' newargs[i+1] = '' elif newargs[i] == '-g': - keep_debug = True + keep_llvm_debug = True + keep_js_debug = True elif newargs[i] == '--bind': bind = True newargs[i] = '' @@ -737,16 +834,16 @@ try: newargs = newargs + [default_cxx_std] if llvm_opts is None: llvm_opts = LLVM_OPT_LEVEL[opt_level] - if llvm_lto is None: llvm_lto = llvm_opts > 0 - if closure is None: closure = 1 if opt_level >= 2 else 0 - if opt_level <= 0: keep_debug = True # always keep debug in -O0 + if llvm_lto is None: llvm_lto = opt_level >= 3 + if opt_level <= 0: keep_llvm_debug = keep_js_debug = True # always keep debug in -O0 + if opt_level > 0: keep_llvm_debug = False # JS optimizer wipes out llvm debug info from being visible + if closure is None and opt_level == 3: closure = True if DEBUG: start_time = time.time() # done after parsing arguments, which might affect debug state if closure: assert os.path.exists(shared.CLOSURE_COMPILER), 'emcc: fatal: Closure compiler (%s) does not exist' % shared.CLOSURE_COMPILER - settings_changes = [] for i in range(len(newargs)): if newargs[i] == '-s': if is_minus_s_for_emcc(newargs, i): @@ -879,7 +976,11 @@ try: shared.Settings.CORRECT_OVERFLOWS = 1 if shared.Settings.CORRECT_SIGNS >= 2 or shared.Settings.CORRECT_OVERFLOWS >= 2 or shared.Settings.CORRECT_ROUNDINGS >= 2: - keep_debug = True # must keep debug info to do line-by-line operations + keep_llvm_debug = True # must keep debug info to do line-by-line operations + + if (keep_llvm_debug or keep_js_debug) and closure: + print >> sys.stderr, 'emcc: warning: disabling closure because debug info was requested' + closure = False if minify_whitespace is None: minify_whitespace = closure # if closure is run, minify whitespace @@ -894,6 +995,7 @@ try: for input_file in input_files: if input_file.endswith(SOURCE_SUFFIXES): if DEBUG: print >> sys.stderr, 'emcc: compiling source file: ', input_file + input_file = shared.Building.preprocess(input_file, in_temp(uniquename(input_file))) output_file = in_temp(unsuffixed(uniquename(input_file)) + '.o') temp_files.append(output_file) args = newargs + ['-emit-llvm', '-c', input_file, '-o', output_file] @@ -927,7 +1029,15 @@ try: # If we were just asked to generate bitcode, stop there if final_suffix not in JS_CONTAINING_SUFFIXES: if llvm_opts > 0: - print >> sys.stderr, 'emcc: warning: -Ox flags ignored, since not generating JavaScript' + if not os.environ.get('EMCC_OPTIMIZE_NORMALLY'): + print >> sys.stderr, 'emcc: warning: -Ox flags ignored, since not generating JavaScript' + else: + for input_file in input_files: + if input_file.endswith(SOURCE_SUFFIXES): + if DEBUG: print >> sys.stderr, 'emcc: optimizing %s with -O%d since EMCC_OPTIMIZE_NORMALLY defined' % (input_file, llvm_opts) + shared.Building.llvm_opt(in_temp(unsuffixed(uniquename(input_file)) + '.o'), llvm_opts) + else: + if DEBUG: print >> sys.stderr, 'emcc: not optimizing %s despite EMCC_OPTIMIZE_NORMALLY since not source code' % (input_file) if not specified_target: for input_file in input_files: shutil.move(in_temp(unsuffixed(uniquename(input_file)) + '.o'), unsuffixed_basename(input_file) + '.' + final_suffix) @@ -959,7 +1069,22 @@ try: def create_libc(): if DEBUG: print >> sys.stderr, 'emcc: building libc for cache' o_s = [] - for src in ['dlmalloc.c', os.path.join('libcxx', 'new.cpp')]: + libc_files = [ + 'dlmalloc.c', + os.path.join('libcxx', 'new.cpp'), + os.path.join('libc', 'stdlib', 'getopt_long.c'), + os.path.join('libc', 'gen', 'err.c'), + os.path.join('libc', 'gen', 'errx.c'), + os.path.join('libc', 'gen', 'warn.c'), + os.path.join('libc', 'gen', 'warnx.c'), + os.path.join('libc', 'gen', 'verr.c'), + os.path.join('libc', 'gen', 'verrx.c'), + os.path.join('libc', 'gen', 'vwarn.c'), + os.path.join('libc', 'gen', 'vwarnx.c'), + os.path.join('libc', 'stdlib', 'strtod.c'), + ]; + + for src in libc_files: o = in_temp(os.path.basename(src) + '.o') execute([shared.PYTHON, shared.EMCC, shared.path_from_root('system', 'lib', src), '-o', o], stdout=stdout, stderr=stderr) o_s.append(o) @@ -984,7 +1109,7 @@ try: def create_libcxx(): if DEBUG: print >> sys.stderr, 'emcc: building libcxx for cache' os = [] - for src in ['algorithm.cpp', 'condition_variable.cpp', 'future.cpp', 'iostream.cpp', 'memory.cpp', 'random.cpp', 'stdexcept.cpp', 'system_error.cpp', 'utility.cpp', 'bind.cpp', 'debug.cpp', 'hash.cpp', 'mutex.cpp', 'string.cpp', 'thread.cpp', 'valarray.cpp', 'chrono.cpp', 'exception.cpp', 'ios.cpp', 'locale.cpp', 'regex.cpp', 'strstream.cpp', 'typeinfo.cpp']: + for src in ['algorithm.cpp', 'condition_variable.cpp', 'future.cpp', 'iostream.cpp', 'memory.cpp', 'random.cpp', 'stdexcept.cpp', 'system_error.cpp', 'utility.cpp', 'bind.cpp', 'debug.cpp', 'hash.cpp', 'mutex.cpp', 'string.cpp', 'thread.cpp', 'valarray.cpp', 'chrono.cpp', 'exception.cpp', 'ios.cpp', 'locale.cpp', 'regex.cpp', 'strstream.cpp']: o = in_temp(src + '.o') execute([shared.PYTHON, shared.EMXX, shared.path_from_root('system', 'lib', 'libcxx', src), '-o', o], stdout=stdout, stderr=stderr) os.append(o) @@ -1003,7 +1128,7 @@ try: def create_libcxxabi(): if DEBUG: print >> sys.stderr, 'emcc: building libcxxabi for cache' os = [] - for src in ['private_typeinfo.cpp']: + for src in ['private_typeinfo.cpp', 'typeinfo.cpp']: o = in_temp(src + '.o') execute([shared.PYTHON, shared.EMXX, shared.path_from_root('system', 'lib', 'libcxxabi', 'src', src), '-o', o], stdout=stdout, stderr=stderr) os.append(o) @@ -1017,29 +1142,32 @@ try: libcxxabi_symbols = filter(lambda symbol: symbol not in libc_symbols, libcxxabi_symbols) libcxxabi_symbols = set(libcxxabi_symbols) - force = False # If we have libcxx, we must force inclusion of libc, since libcxx uses new internally. Note: this is kind of hacky - + # If we have libcxx, we must force inclusion of libc, since libcxx uses new internally. Note: this is kind of hacky + # Settings this in the environment will avoid checking dependencies and make building big projects a little faster + force = os.environ.get('EMCC_FORCE_STDLIBS') + has = need = None for name, create, fix, library_symbols in [('libcxx', create_libcxx, fix_libcxx, libcxx_symbols), ('libcxxabi', create_libcxxabi, fix_libcxxabi, libcxxabi_symbols), ('libc', create_libc, fix_libc, libc_symbols)]: - need = set() - has = set() - for temp_file in temp_files: - symbols = shared.Building.llvm_nm(temp_file) - for library_symbol in library_symbols: - if library_symbol in symbols.undefs: - need.add(library_symbol) - if library_symbol in symbols.defs: - has.add(library_symbol) - for haz in has: # remove symbols that are supplied by another of the inputs - if haz in need: - need.remove(haz) - if DEBUG: print >> sys.stderr, 'emcc: considering including %s: we need %s and have %s' % (name, str(need), str(has)) + if not force: + need = set() + has = set() + for temp_file in temp_files: + symbols = shared.Building.llvm_nm(temp_file) + for library_symbol in library_symbols: + if library_symbol in symbols.undefs: + need.add(library_symbol) + if library_symbol in symbols.defs: + has.add(library_symbol) + for haz in has: # remove symbols that are supplied by another of the inputs + if haz in need: + need.remove(haz) + if DEBUG: print >> sys.stderr, 'emcc: considering including %s: we need %s and have %s' % (name, str(need), str(has)) if force or len(need) > 0: # We need to build and link the library in if DEBUG: print >> sys.stderr, 'emcc: including %s' % name libfile = shared.Cache.get(name, create) - if len(has) > 0: + if has and len(has) > 0: # remove the symbols we do not need fixed = in_temp(uniquename(libfile)) + '.bc' shutil.copyfile(libfile, fixed) @@ -1049,7 +1177,7 @@ try: libfile = fixed extra_files_to_link.append(libfile) force = True - if fix: + if fix and need: fix(need) # First, combine the bitcode files if there are several. We must also link if we have a singleton .a @@ -1057,7 +1185,10 @@ try: (not LEAVE_INPUTS_RAW and not (suffix(temp_files[0]) in BITCODE_SUFFIXES or suffix(temp_files[0]) in DYNAMICLIB_SUFFIXES) and shared.Building.is_ar(temp_files[0])): linker_inputs = temp_files + extra_files_to_link if DEBUG: print >> sys.stderr, 'emcc: linking: ', linker_inputs + t0 = time.time() shared.Building.link(linker_inputs, in_temp(target_basename + '.bc')) + t1 = time.time() + if DEBUG: print >> sys.stderr, 'emcc: linking took %.2f seconds' % (t1 - t0) final = in_temp(target_basename + '.bc') else: if not LEAVE_INPUTS_RAW: @@ -1084,11 +1215,14 @@ try: # Optimize, if asked to if not LEAVE_INPUTS_RAW: - link_opts = [] if keep_debug else ['-strip-debug'] + link_opts = [] if keep_llvm_debug else ['-strip-debug'] # remove LLVM debug info in -O1+, since the optimizer removes it anyhow if llvm_opts > 0: - shared.Building.llvm_opt(in_temp(target_basename + '.bc'), llvm_opts) - if DEBUG: save_intermediate('opt', 'bc') - # Do LTO in a separate pass to work around LLVM bug XXX (see failure e.g. in cubescript) + if not os.environ.get('EMCC_OPTIMIZE_NORMALLY'): + shared.Building.llvm_opt(in_temp(target_basename + '.bc'), llvm_opts) + if DEBUG: save_intermediate('opt', 'bc') + # Do LTO in a separate pass to work around LLVM bug XXX (see failure e.g. in cubescript) + else: + if DEBUG: print >> sys.stderr, 'emcc: not running opt because EMCC_OPTIMIZE_NORMALLY was specified, opt should have been run before' if shared.Building.can_build_standalone(): # If we can LTO, do it before dce, since it opens up dce opportunities if llvm_lto and shared.Building.can_use_unsafe_opts(): @@ -1167,15 +1301,12 @@ try: execute(shlex.split(js_transform, posix=posix) + [os.path.abspath(final)]) if DEBUG: save_intermediate('transformed') - if shared.Settings.ASM_JS: # XXX temporary wrapping for testing purposes - print >> sys.stderr, 'emcc: ASM_JS mode is highly experimental, and will not work on most codebases yet. It is NOT recommended that you try this yet.' - # It is useful to run several js optimizer passes together, to save on unneeded unparsing/reparsing js_optimizer_queue = [] def flush_js_optimizer_queue(): global final, js_optimizer_queue if len(js_optimizer_queue) > 0 and not(len(js_optimizer_queue) == 1 and js_optimizer_queue[0] == 'last'): - if DEBUG < 2: + if DEBUG != '2': if shared.Settings.ASM_JS: js_optimizer_queue = ['asm'] + js_optimizer_queue if DEBUG: print >> sys.stderr, 'emcc: applying js optimization passes:', js_optimizer_queue @@ -1194,7 +1325,7 @@ try: if opt_level >= 1: if DEBUG: print >> sys.stderr, 'emcc: running pre-closure post-opts' - if DEBUG >= 2: + if DEBUG == '2': # Clean up the syntax a bit final = shared.Building.js_optimizer(final, [], jcache) if DEBUG: save_intermediate('pretty') @@ -1216,8 +1347,9 @@ try: if DEBUG: print >> sys.stderr, 'emcc: running closure' final = shared.Building.closure_compiler(final) if DEBUG: save_intermediate('closure') - elif shared.Settings.ASM_JS and shared.Settings.RELOOP: - js_optimizer_queue += ['registerize'] # we can't use closure in asm, but this does much of the same + elif shared.Settings.RELOOP and not closure and not keep_js_debug: + # do this if closure is not enabled (it gives similar speedups), and we do not need to keep debug info around + js_optimizer_queue += ['registerize'] if opt_level >= 1: if DEBUG: print >> sys.stderr, 'emcc: running post-closure post-opts' @@ -1230,6 +1362,12 @@ try: flush_js_optimizer_queue() + if not minify_whitespace: + # Remove some trivial whitespace + src = open(final).read() + src = re.sub(r'\n+[ \n]*\n+', '\n', src) + open(final, 'w').write(src) + # If we were asked to also generate HTML, do that if final_suffix == 'html': if DEBUG: print >> sys.stderr, 'emcc: generating HTML' diff --git a/emscripten.py b/emscripten.py index af762a21..b698654b 100755 --- a/emscripten.py +++ b/emscripten.py @@ -9,18 +9,9 @@ header files (so that the JS compiler can see the constants in those headers, for the libc implementation in JS). ''' -import os, sys, json, optparse, subprocess, re, time, multiprocessing +import os, sys, json, optparse, subprocess, re, time, multiprocessing, functools -if not os.environ.get('EMSCRIPTEN_SUPPRESS_USAGE_WARNING'): - print >> sys.stderr, ''' -============================================================== -WARNING: You should normally never use this! Use emcc instead. -============================================================== - ''' - -from tools import shared - -DEBUG = os.environ.get('EMCC_DEBUG') +from tools import jsrun, cache as cache_module, tempfiles __rootpath__ = os.path.abspath(os.path.dirname(__file__)) def path_from_root(*pathelems): @@ -29,11 +20,6 @@ def path_from_root(*pathelems): """ return os.path.join(__rootpath__, *pathelems) -temp_files = shared.TempFiles() - -compiler_engine = None -jcache = False - def scan(ll, settings): # blockaddress(@main, %23) blockaddrs = [] @@ -43,20 +29,25 @@ def scan(ll, settings): if len(blockaddrs) > 0: settings['NECESSARY_BLOCKADDRS'] = blockaddrs -NUM_CHUNKS_PER_CORE = 5 +NUM_CHUNKS_PER_CORE = 1.25 MIN_CHUNK_SIZE = 1024*1024 MAX_CHUNK_SIZE = float(os.environ.get('EMSCRIPT_MAX_CHUNK_SIZE') or 'inf') # configuring this is just for debugging purposes -def process_funcs(args): - i, funcs, meta, settings_file, compiler, forwarded_file, libraries = args +def process_funcs((i, funcs, meta, settings_file, compiler, forwarded_file, libraries, compiler_engine, temp_files)): ll = ''.join(funcs) + '\n' + meta funcs_file = temp_files.get('.func_%d.ll' % i).name open(funcs_file, 'w').write(ll) - out = shared.run_js(compiler, compiler_engine, [settings_file, funcs_file, 'funcs', forwarded_file] + libraries, stdout=subprocess.PIPE, cwd=path_from_root('src')) - shared.try_delete(funcs_file) + out = jsrun.run_js( + compiler, + engine=compiler_engine, + args=[settings_file, funcs_file, 'funcs', forwarded_file] + libraries, + stdout=subprocess.PIPE, + cwd=path_from_root('src')) + tempfiles.try_delete(funcs_file) return out -def emscript(infile, settings, outfile, libraries=[]): +def emscript(infile, settings, outfile, libraries=[], compiler_engine=None, + jcache=None, temp_files=None, DEBUG=None, DEBUG_CACHE=None): """Runs the emscripten LLVM-to-JS compiler. We parallelize as much as possible Args: @@ -75,7 +66,7 @@ def emscript(infile, settings, outfile, libraries=[]): if DEBUG: print >> sys.stderr, 'emscript: ll=>js' - if jcache: shared.JCache.ensure() + if jcache: jcache.ensure() # Pre-scan ll and alter settings as necessary if DEBUG: t = time.time() @@ -131,7 +122,7 @@ def emscript(infile, settings, outfile, libraries=[]): settings_file = temp_files.get('.txt').name def save_settings(): global settings_text - settings_text = json.dumps(settings) + settings_text = json.dumps(settings, sort_keys=True) s = open(settings_file, 'w') s.write(settings_text) s.close() @@ -144,15 +135,30 @@ def emscript(infile, settings, outfile, libraries=[]): out = None if jcache: keys = [pre_input, settings_text, ','.join(libraries)] - shortkey = shared.JCache.get_shortkey(keys) - out = shared.JCache.get(shortkey, keys) + shortkey = jcache.get_shortkey(keys) + if DEBUG_CACHE: print >>sys.stderr, 'shortkey', shortkey + + out = jcache.get(shortkey, keys) + + if DEBUG_CACHE and not out: + dfpath = os.path.join(configuration.TEMP_DIR, "ems_" + shortkey) + dfp = open(dfpath, 'w') + dfp.write(pre_input); + dfp.write("\n\n========================== settings_text\n\n"); + dfp.write(settings_text); + dfp.write("\n\n========================== libraries\n\n"); + dfp.write("\n".join(libraries)) + dfp.close() + print >>sys.stderr, ' cache miss, key data dumped to %s' % dfpath + if out and DEBUG: print >> sys.stderr, ' loading pre from jcache' if not out: open(pre_file, 'w').write(pre_input) - out = shared.run_js(compiler, shared.COMPILER_ENGINE, [settings_file, pre_file, 'pre'] + libraries, stdout=subprocess.PIPE, cwd=path_from_root('src')) + out = jsrun.run_js(compiler, compiler_engine, [settings_file, pre_file, 'pre'] + libraries, stdout=subprocess.PIPE, + cwd=path_from_root('src')) if jcache: if DEBUG: print >> sys.stderr, ' saving pre to jcache' - shared.JCache.set(shortkey, keys, out) + jcache.set(shortkey, keys, out) pre, forwarded_data = out.split('//FORWARDED_DATA:') forwarded_file = temp_files.get('.json').name open(forwarded_file, 'w').write(forwarded_data) @@ -160,12 +166,12 @@ def emscript(infile, settings, outfile, libraries=[]): # Phase 2 - func - cores = multiprocessing.cpu_count() + cores = int(os.environ.get('EMCC_CORES') or multiprocessing.cpu_count()) assert cores >= 1 if cores > 1: - intended_num_chunks = cores * NUM_CHUNKS_PER_CORE + intended_num_chunks = int(round(cores * NUM_CHUNKS_PER_CORE)) chunk_size = max(MIN_CHUNK_SIZE, total_ll_size / intended_num_chunks) - chunk_size += 3*len(meta) # keep ratio of lots of function code to meta (expensive to process, and done in each parallel task) + chunk_size += 3*len(meta) + len(forwarded_data)/3 # keep ratio of lots of function code to meta (expensive to process, and done in each parallel task) and forwarded data (less expensive but potentially significant) chunk_size = min(MAX_CHUNK_SIZE, chunk_size) else: chunk_size = MAX_CHUNK_SIZE # if 1 core, just use the max chunk size @@ -177,15 +183,17 @@ def emscript(infile, settings, outfile, libraries=[]): settings['EXPORTED_FUNCTIONS'] = forwarded_json['EXPORTED_FUNCTIONS'] save_settings() - chunks = shared.JCache.chunkify(funcs, chunk_size, 'emscript_files' if jcache else None) + chunks = cache_module.chunkify( + funcs, chunk_size, + jcache.get_cachename('emscript_files') if jcache else None) if jcache: # load chunks from cache where we can # TODO: ignore small chunks cached_outputs = [] def load_from_cache(chunk): keys = [settings_text, forwarded_data, chunk] - shortkey = shared.JCache.get_shortkey(keys) # TODO: share shortkeys with later code - out = shared.JCache.get(shortkey, keys) # this is relatively expensive (pickling?) + shortkey = jcache.get_shortkey(keys) # TODO: share shortkeys with later code + out = jcache.get(shortkey, keys) # this is relatively expensive (pickling?) if out: cached_outputs.append(out) return False @@ -198,12 +206,16 @@ def emscript(infile, settings, outfile, libraries=[]): # TODO: minimize size of forwarded data from funcs to what we actually need - if cores == 1 and total_ll_size < MAX_CHUNK_SIZE: assert len(chunks) == 1, 'no point in splitting up without multiple cores' + if cores == 1 and total_ll_size < MAX_CHUNK_SIZE: + assert len(chunks) == 1, 'no point in splitting up without multiple cores' if len(chunks) > 0: if DEBUG: print >> sys.stderr, ' emscript: phase 2 working on %d chunks %s (intended chunk size: %.2f MB, meta: %.2f MB, forwarded: %.2f MB, total: %.2f MB)' % (len(chunks), ('using %d cores' % cores) if len(chunks) > 1 else '', chunk_size/(1024*1024.), len(meta)/(1024*1024.), len(forwarded_data)/(1024*1024.), total_ll_size/(1024*1024.)) - commands = [(i, chunks[i], meta, settings_file, compiler, forwarded_file, libraries) for i in range(len(chunks))] + commands = [ + (i, chunk, meta, settings_file, compiler, forwarded_file, libraries, compiler_engine, temp_files) + for i, chunk in enumerate(chunks) + ] if len(chunks) > 1: pool = multiprocessing.Pool(processes=cores) @@ -218,13 +230,15 @@ def emscript(infile, settings, outfile, libraries=[]): for i in range(len(chunks)): chunk = chunks[i] keys = [settings_text, forwarded_data, chunk] - shortkey = shared.JCache.get_shortkey(keys) - shared.JCache.set(shortkey, keys, outputs[i]) + shortkey = jcache.get_shortkey(keys) + jcache.set(shortkey, keys, outputs[i]) if out and DEBUG and len(chunks) > 0: print >> sys.stderr, ' saving %d funcchunks to jcache' % len(chunks) if jcache: outputs += cached_outputs # TODO: preserve order outputs = [output.split('//FORWARDED_DATA:') for output in outputs] + for output in outputs: + assert len(output) == 2, 'Did not receive forwarded data in an output - process failed? We only got: ' + output[0][-3000:] if DEBUG: print >> sys.stderr, ' emscript: phase 2 took %s seconds' % (time.time() - t) if DEBUG: t = time.time() @@ -292,8 +306,9 @@ def emscript(infile, settings, outfile, libraries=[]): if DEBUG: t = time.time() post_file = temp_files.get('.post.ll').name open(post_file, 'w').write('\n') # no input, just processing of forwarded data - out = shared.run_js(compiler, shared.COMPILER_ENGINE, [settings_file, post_file, 'post', forwarded_file] + libraries, stdout=subprocess.PIPE, cwd=path_from_root('src')) - post, last_forwarded_data = out.split('//FORWARDED_DATA:') + out = jsrun.run_js(compiler, compiler_engine, [settings_file, post_file, 'post', forwarded_file] + libraries, stdout=subprocess.PIPE, + cwd=path_from_root('src')) + post, last_forwarded_data = out.split('//FORWARDED_DATA:') # if this fails, perhaps the process failed prior to printing forwarded data? last_forwarded_json = json.loads(last_forwarded_data) if settings.get('ASM_JS'): @@ -317,20 +332,18 @@ def emscript(infile, settings, outfile, libraries=[]): params = ','.join(['p%d' % p for p in range(len(sig)-1)]) coercions = ';'.join(['p%d = %sp%d%s' % (p, '+' if sig[p+1] != 'i' else '', p, '' if sig[p+1] != 'i' else '|0') for p in range(len(sig)-1)]) + ';' ret = '' if sig[0] == 'v' else ('return %s0' % ('+' if sig[0] != 'i' else '')) - return ('function %s(%s) { %s abort(%d); %s };' % (bad, params, coercions, i, ret), raw.replace('[0,', '[' + bad + ',').replace(',0,', ',' + bad + ',').replace(',0,', ',' + bad + ',').replace(',0]', ',' + bad + ']').replace(',0]', ',' + bad + ']')) + return ('function %s(%s) { %s abort(%d); %s };' % (bad, params, coercions, i, ret), raw.replace('[0,', '[' + bad + ',').replace(',0,', ',' + bad + ',').replace(',0,', ',' + bad + ',').replace(',0]', ',' + bad + ']').replace(',0]', ',' + bad + ']').replace(',0\n', ',' + bad + '\n')) infos = [make_table(sig, raw) for sig, raw in last_forwarded_json['Functions']['tables'].iteritems()] function_tables_defs = '\n'.join([info[0] for info in infos] + [info[1] for info in infos]) asm_setup = '' - maths = ['Math.' + func for func in ['floor', 'abs', 'sqrt', 'pow', 'cos', 'sin', 'tan', 'acos', 'asin', 'atan', 'atan2', 'exp', 'log', 'ceil']] - if settings['USE_MATH_IMUL']: - maths += ['Math.imul'] - asm_setup += 'if (!Math.imul) Math.imul = function(x, y) { return (x*y)|0 }; // # not a real polyfill since semantics not identical, but close and fairly fast\n' + maths = ['Math.' + func for func in ['floor', 'abs', 'sqrt', 'pow', 'cos', 'sin', 'tan', 'acos', 'asin', 'atan', 'atan2', 'exp', 'log', 'ceil', 'imul']] fundamentals = ['Math', 'Int8Array', 'Int16Array', 'Int32Array', 'Uint8Array', 'Uint16Array', 'Uint32Array', 'Float32Array', 'Float64Array'] math_envs = ['Runtime.bitshift64', 'Math.min'] # TODO: move min to maths asm_setup += '\n'.join(['var %s = %s;' % (f.replace('.', '_'), f) for f in math_envs]) basic_funcs = ['abort', 'assert', 'asmPrintInt', 'asmPrintFloat', 'copyTempDouble', 'copyTempFloat'] + [m.replace('.', '_') for m in math_envs] if settings['SAFE_HEAP']: basic_funcs += ['SAFE_HEAP_LOAD', 'SAFE_HEAP_STORE', 'SAFE_HEAP_CLEAR'] + if settings['CHECK_HEAP_ALIGN']: basic_funcs += ['CHECK_ALIGN_2', 'CHECK_ALIGN_4', 'CHECK_ALIGN_8'] basic_vars = ['STACKTOP', 'STACK_MAX', 'tempDoublePtr', 'ABORT'] basic_float_vars = ['NaN', 'Infinity'] if forwarded_json['Types']['preciseI64MathUsed']: @@ -478,8 +491,7 @@ Runtime.stackRestore = function(top) { asm.stackRestore(top) }; outfile.close() - -def main(args): +def main(args, compiler_engine, cache, jcache, relooper, temp_files, DEBUG, DEBUG_CACHE): # Prepare settings for serialization to JSON. settings = {} for setting in args.settings: @@ -553,16 +565,23 @@ def main(args): libraries = args.libraries[0].split(',') if len(args.libraries) > 0 else [] # Compile the assembly to Javascript. - if settings.get('RELOOP'): shared.Building.ensure_relooper() - - emscript(args.infile, settings, args.outfile, libraries) - -if __name__ == '__main__': + if settings.get('RELOOP'): + if not relooper: + relooper = cache.get_path('relooper.js') + settings.setdefault('RELOOPER', relooper) + if not os.path.exists(relooper): + from tools import shared + shared.Building.ensure_relooper(relooper) + + emscript(args.infile, settings, args.outfile, libraries, compiler_engine=compiler_engine, + jcache=jcache, temp_files=temp_files, DEBUG=DEBUG, DEBUG_CACHE=DEBUG_CACHE) + +def _main(environ): parser = optparse.OptionParser( - usage='usage: %prog [-h] [-H HEADERS] [-o OUTFILE] [-c COMPILER_ENGINE] [-s FOO=BAR]* infile', - description=('You should normally never use this! Use emcc instead. ' - 'This is a wrapper around the JS compiler, converting .ll to .js.'), - epilog='') + usage='usage: %prog [-h] [-H HEADERS] [-o OUTFILE] [-c COMPILER_ENGINE] [-s FOO=BAR]* infile', + description=('You should normally never use this! Use emcc instead. ' + 'This is a wrapper around the JS compiler, converting .ll to .js.'), + epilog='') parser.add_option('-H', '--headers', default=[], action='append', @@ -575,8 +594,11 @@ if __name__ == '__main__': default=sys.stdout, help='Where to write the output; defaults to stdout.') parser.add_option('-c', '--compiler', - default=shared.COMPILER_ENGINE, + default=None, help='Which JS engine to use to run the compiler; defaults to the one in ~/.emscripten.') + parser.add_option('--relooper', + default=None, + help='Which relooper file to use if RELOOP is enabled.') parser.add_option('-s', '--setting', dest='settings', default=[], @@ -588,16 +610,82 @@ if __name__ == '__main__': action='store_true', default=False, help=('Enable jcache (ccache-like caching of compilation results, for faster incremental builds).')) + parser.add_option('-T', '--temp-dir', + default=None, + help=('Where to create temporary files.')) + parser.add_option('-v', '--verbose', + action='store_true', + dest='verbose', + help='Displays debug output') + parser.add_option('-q', '--quiet', + action='store_false', + dest='verbose', + help='Hides debug output') + parser.add_option('--suppressUsageWarning', + action='store_true', + default=environ.get('EMSCRIPTEN_SUPPRESS_USAGE_WARNING'), + help=('Suppress usage warning')) # Convert to the same format that argparse would have produced. keywords, positional = parser.parse_args() + + if not keywords.suppressUsageWarning: + print >> sys.stderr, ''' +============================================================== +WARNING: You should normally never use this! Use emcc instead. +============================================================== + ''' + if len(positional) != 1: raise RuntimeError('Must provide exactly one positional argument.') keywords.infile = os.path.abspath(positional[0]) if isinstance(keywords.outfile, basestring): keywords.outfile = open(keywords.outfile, 'w') - compiler_engine = keywords.compiler - jcache = keywords.jcache - temp_files.run_and_clean(lambda: main(keywords)) + if keywords.relooper: + relooper = os.path.abspath(keywords.relooper) + else: + relooper = None # use the cache + + def get_configuration(): + if hasattr(get_configuration, 'configuration'): + return get_configuration.configuration + + from tools import shared + configuration = shared.Configuration(environ=os.environ) + get_configuration.configuration = configuration + return configuration + + if keywords.temp_dir is None: + temp_files = get_configuration().get_temp_files() + else: + temp_dir = os.path.abspath(keywords.temp_dir) + if not os.path.exists(temp_dir): + os.makedirs(temp_dir) + temp_files = tempfiles.TempFiles(temp_dir) + + if keywords.compiler is None: + from tools import shared + keywords.compiler = shared.COMPILER_ENGINE + + if keywords.verbose is None: + DEBUG = get_configuration().DEBUG + DEBUG_CACHE = get_configuration().DEBUG_CACHE + else: + DEBUG = keywords.verbose + DEBUG_CACHE = keywords.verbose + + cache = cache_module.Cache() + temp_files.run_and_clean(lambda: main( + keywords, + compiler_engine=keywords.compiler, + cache=cache, + jcache=cache_module.JCache(cache) if keywords.jcache else None, + relooper=relooper, + temp_files=temp_files, + DEBUG=DEBUG, + DEBUG_CACHE=DEBUG_CACHE, + )) +if __name__ == '__main__': + _main(environ=os.environ) diff --git a/src/analyzer.js b/src/analyzer.js index 1c53b76c..209e3140 100644 --- a/src/analyzer.js +++ b/src/analyzer.js @@ -18,6 +18,7 @@ function recomputeLines(func) { // Handy sets var BRANCH_INVOKE = set('branch', 'invoke'); +var LABEL_ENDERS = set('branch', 'return'); var SIDE_EFFECT_CAUSERS = set('call', 'invoke', 'atomic'); var UNUNFOLDABLE = set('value', 'structvalue', 'type', 'phiparam'); @@ -88,7 +89,7 @@ function analyzer(data, sidePass) { // Internal line if (!currLabelFinished) { item.functions.slice(-1)[0].labels.slice(-1)[0].lines.push(subItem); // If this line fails, perhaps missing a label? - if (subItem.intertype === 'branch') { + if (subItem.intertype in LABEL_ENDERS) { currLabelFinished = true; } } else { @@ -121,7 +122,8 @@ function analyzer(data, sidePass) { // Legalization if (USE_TYPED_ARRAYS == 2) { function getLegalVars(base, bits, allowLegal) { - if (allowLegal && bits <= 32) return [{ ident: base, bits: bits }]; + bits = bits || 32; // things like pointers are all i32, but show up as 0 bits from getBits + if (allowLegal && bits <= 32) return [{ ident: base + ('i' + bits in Runtime.INT_TYPES ? '' : '$0'), bits: bits }]; if (isNumber(base)) return getLegalLiterals(base, bits); var ret = new Array(Math.ceil(bits/32)); var i = 0; @@ -223,6 +225,9 @@ function analyzer(data, sidePass) { for (var i = 0; i < item.params.length; i++) { if (item.params[i].type == 'i64') item.params[i].type = 'i32'; } + } else if (item.intertype == 'inttoptr') { + var input = item.params[0]; + if (input.type == 'i64') input.type = 'i32'; // inttoptr can only care about 32 bits anyhow since pointers are 32-bit } if (isIllegalType(item.valueType) || isIllegalType(item.type)) { isIllegal = true; @@ -285,7 +290,7 @@ function analyzer(data, sidePass) { var elements = getLegalParams([item.value], bits)[0]; var j = 0; elements.forEach(function(element) { - var tempVar = '$st$' + i + '$' + j; + var tempVar = '$st$' + (tempId++) + '$' + j; toAdd.push({ intertype: 'getelementptr', assignTo: tempVar, @@ -396,7 +401,7 @@ function analyzer(data, sidePass) { var j = 0; var toAdd = []; elements.forEach(function(element) { - var tempVar = '$st$' + i + '$' + j; + var tempVar = '$ld$' + (tempId++) + '$' + j; toAdd.push({ intertype: 'getelementptr', assignTo: tempVar, @@ -643,13 +648,7 @@ function analyzer(data, sidePass) { default: throw 'Invalid mathop for legalization: ' + [value.op, item.lineNum, dump(item)]; } // Do the legalization - var sourceElements; - if (sourceBits <= 32) { - // The input is a legal type - sourceElements = [{ ident: value.params[0].ident, bits: sourceBits }]; - } else { - sourceElements = getLegalVars(value.params[0].ident, sourceBits); - } + var sourceElements = getLegalVars(value.params[0].ident, sourceBits, true); if (!isNumber(shifts)) { // We can't statically legalize this, do the operation at runtime TODO: optimize assert(sourceBits == 64, 'TODO: handle nonconstant shifts on != 64 bits'); @@ -681,9 +680,9 @@ function analyzer(data, sidePass) { params: [(signed && j + whole > sourceElements.length) ? signedKeepAlive : null], type: 'i32', }; - if (j == 0 && isUnsignedOp(value.op) && sourceBits < 32) { + if (j == 0 && sourceBits < 32) { // zext sign correction - result.ident = makeSignOp(result.ident, 'i' + sourceBits, 'un', 1, 1); + result.ident = makeSignOp(result.ident, 'i' + sourceBits, isUnsignedOp(value.op) ? 'un' : 're', 1, 1); } if (fraction != 0) { var other = { @@ -953,6 +952,7 @@ function analyzer(data, sidePass) { // Function parameters func.params.forEach(function(param) { if (param.intertype !== 'varargs') { + if (func.variables[param.ident]) warn('cannot have duplicate variable names: ' + param.ident); // toNiceIdent collisions? func.variables[param.ident] = { ident: param.ident, type: param.type, @@ -966,6 +966,7 @@ function analyzer(data, sidePass) { // Normal variables func.lines.forEach(function(item, i) { if (item.assignTo) { + if (func.variables[item.assignTo]) warn('cannot have duplicate variable names: ' + item.assignTo); // toNiceIdent collisions? var variable = func.variables[item.assignTo] = { ident: item.assignTo, type: item.type, @@ -1381,7 +1382,7 @@ function analyzer(data, sidePass) { var label = func.labels[i]; for (var j = 0; j < label.lines.length; j++) { var line = label.lines[j]; - if (line.intertype == 'call' && line.ident == setjmp) { + if ((line.intertype == 'call' || line.intertype == 'invoke') && line.ident == setjmp) { // Add a new label var oldIdent = label.ident; var newIdent = func.labelIdCounter++; diff --git a/src/compiler.js b/src/compiler.js index 25c306cf..3047daf1 100644 --- a/src/compiler.js +++ b/src/compiler.js @@ -160,12 +160,6 @@ if (SAFE_HEAP >= 2) { SAFE_HEAP_LINES = set(SAFE_HEAP_LINES); // for fast checking } -if (PGO) { // by default, correct everything during PGO - CORRECT_SIGNS = CORRECT_SIGNS || 1; - CORRECT_OVERFLOWS = CORRECT_OVERFLOWS || 1; - CORRECT_ROUNDINGS = CORRECT_ROUNDINGS || 1; -} - EXPORTED_FUNCTIONS = set(EXPORTED_FUNCTIONS); EXPORTED_GLOBALS = set(EXPORTED_GLOBALS); EXCEPTION_CATCHING_WHITELIST = set(EXCEPTION_CATCHING_WHITELIST); @@ -178,20 +172,21 @@ assert(!(USE_TYPED_ARRAYS === 2 && QUANTUM_SIZE !== 4), 'For USE_TYPED_ARRAYS == if (ASM_JS) { assert(!ALLOW_MEMORY_GROWTH, 'Cannot grow asm.js heap'); assert((TOTAL_MEMORY&(TOTAL_MEMORY-1)) == 0, 'asm.js heap must be power of 2'); + assert(DISABLE_EXCEPTION_CATCHING == 1, 'asm.js does not support C++ exceptions yet'); } assert(!(!NAMED_GLOBALS && BUILD_AS_SHARED_LIB)); // shared libraries must have named globals // Output some info and warnings based on settings if (phase == 'pre') { - if (!MICRO_OPTS || !RELOOP || ASSERTIONS || CHECK_SIGNS || CHECK_OVERFLOWS || INIT_STACK || INIT_HEAP || - !SKIP_STACK_IN_SMALL || SAFE_HEAP || PGO || PROFILE || !DISABLE_EXCEPTION_CATCHING) { + if (!MICRO_OPTS || !RELOOP || ASSERTIONS || CHECK_SIGNS || CHECK_OVERFLOWS || INIT_HEAP || + !SKIP_STACK_IN_SMALL || SAFE_HEAP || !DISABLE_EXCEPTION_CATCHING) { print('// Note: Some Emscripten settings will significantly limit the speed of the generated code.'); } else { print('// Note: For maximum-speed code, see "Optimizing Code" on the Emscripten wiki, https://github.com/kripken/emscripten/wiki/Optimizing-Code'); } - if (DOUBLE_MODE || CORRECT_SIGNS || CORRECT_OVERFLOWS || CORRECT_ROUNDINGS) { + if (DOUBLE_MODE || CORRECT_SIGNS || CORRECT_OVERFLOWS || CORRECT_ROUNDINGS || CHECK_HEAP_ALIGN) { print('// Note: Some Emscripten settings may limit the speed of the generated code.'); } } @@ -204,7 +199,7 @@ load('parseTools.js'); load('intertyper.js'); load('analyzer.js'); load('jsifier.js'); -if (RELOOP) load('relooper.js') +if (RELOOP) load(RELOOPER) globalEval(processMacros(preprocess(read('runtime.js')))); Runtime.QUANTUM_SIZE = QUANTUM_SIZE; diff --git a/src/corruptionCheck.js b/src/corruptionCheck.js new file mode 100644 index 00000000..315f5cf0 --- /dev/null +++ b/src/corruptionCheck.js @@ -0,0 +1,98 @@ + +// See settings.js, CORRUPTION_CHECK + +var CorruptionChecker = { + BUFFER_FACTOR: Math.round({{{ CORRUPTION_CHECK }}}), + + ptrs: {}, + checks: 0, + checkFrequency: 1, + + init: function() { + this.realMalloc = _malloc; + _malloc = Module['_malloc'] = this.malloc; + + this.realFree = _free; + _free = Module['_free'] = this.free; + + if (typeof _realloc != 'undefined') { + this.realRealloc = _realloc; + _realloc = Module['_realloc'] = this.realloc; + } + + __ATEXIT__.push({ func: function() { + Module.printErr('No corruption detected, ran ' + CorruptionChecker.checks + ' checks.'); + } }); + }, + malloc: function(size) { + if (size <= 0) size = 1; // malloc(0) sometimes happens - just allocate a larger area, no harm + CorruptionChecker.checkAll(); + size = (size+7)&(~7); + var allocation = CorruptionChecker.realMalloc(size*(1+2*CorruptionChecker.BUFFER_FACTOR)); + var ptr = allocation + size*CorruptionChecker.BUFFER_FACTOR; + assert(!CorruptionChecker.ptrs[ptr]); + CorruptionChecker.ptrs[ptr] = size; + CorruptionChecker.fillBuffer(allocation, size*CorruptionChecker.BUFFER_FACTOR); + CorruptionChecker.fillBuffer(allocation + size*(1+CorruptionChecker.BUFFER_FACTOR), size*CorruptionChecker.BUFFER_FACTOR); + //Module.printErr('malloc ' + size + ' ==> ' + [ptr, allocation]); + return ptr; + }, + free: function(ptr) { + if (!ptr) return; // ok to free(NULL), does nothing + CorruptionChecker.checkAll(); + var size = CorruptionChecker.ptrs[ptr]; + //Module.printErr('free ' + ptr + ' of size ' + size); + assert(size); + var allocation = ptr - size*CorruptionChecker.BUFFER_FACTOR; + //Module.printErr('free ' + ptr + ' of size ' + size + ' and allocation ' + allocation); + delete CorruptionChecker.ptrs[ptr]; + CorruptionChecker.realFree(allocation); + }, + realloc: function(ptr, newSize) { + //Module.printErr('realloc ' + ptr + ' to size ' + newSize); + if (newSize <= 0) newSize = 1; // like in malloc + if (!ptr) return CorruptionChecker.malloc(newSize); // realloc(NULL, size) forwards to malloc according to the spec + var size = CorruptionChecker.ptrs[ptr]; + assert(size); + var allocation = ptr - size*CorruptionChecker.BUFFER_FACTOR; + var newPtr = CorruptionChecker.malloc(newSize); + //Module.printErr('realloc ' + ptr + ' to size ' + newSize + ' is now ' + newPtr); + var newAllocation = newPtr + newSize*CorruptionChecker.BUFFER_FACTOR; + HEAPU8.set(HEAPU8.subarray(ptr, ptr + Math.min(size, newSize)), newPtr); + CorruptionChecker.free(ptr); + return newPtr; + }, + canary: function(x) { + return (x&127) + 10; + }, + fillBuffer: function(buffer, size) { + for (var x = buffer; x < buffer + size; x++) { + {{{ makeSetValue('x', 0, 'CorruptionChecker.canary(x)', 'i8') }}}; + } + }, + checkBuffer: function(buffer, size) { + for (var x = buffer; x < buffer + size; x++) { + if (({{{ makeGetValue('x', 0, 'i8') }}}&255) != CorruptionChecker.canary(x)) { + assert(0, 'Heap corruption detected!' + [x, buffer, size, {{{ makeGetValue('x', 0, 'i8') }}}&255, CorruptionChecker.canary(x)]); + } + } + }, + checkPtr: function(ptr) { + var size = CorruptionChecker.ptrs[ptr]; + assert(size); + var allocation = ptr - size*CorruptionChecker.BUFFER_FACTOR; + CorruptionChecker.checkBuffer(allocation, size*CorruptionChecker.BUFFER_FACTOR); + CorruptionChecker.checkBuffer(allocation + size*(1+CorruptionChecker.BUFFER_FACTOR), size*CorruptionChecker.BUFFER_FACTOR); + }, + checkAll: function(force) { + CorruptionChecker.checks++; + if (!force && CorruptionChecker.checks % CorruptionChecker.checkFrequency != 0) return; + //Module.printErr('checking for corruption ' + (CorruptionChecker.checks/CorruptionChecker.checkFrequency)); + for (var ptr in CorruptionChecker.ptrs) { + CorruptionChecker.checkPtr(ptr, false); + } + }, +}; + +CorruptionChecker.init(); + diff --git a/src/experimental/allow_loopvars_from_memsetcpy_inasm.diff b/src/experimental/allow_loopvars_from_memsetcpy_inasm.diff new file mode 100644 index 00000000..a2dde7da --- /dev/null +++ b/src/experimental/allow_loopvars_from_memsetcpy_inasm.diff @@ -0,0 +1,97 @@ +commit a61ef3dbbaf7333ad67fca29c0aad5bcc99b653a +Author: Alon Zakai <alonzakai@gmail.com> +Date: Wed Mar 6 18:18:03 2013 -0800 + + handle new vars in asm code, such as the loop vars from memset/memcpy loops + +diff --git a/tools/js-optimizer.js b/tools/js-optimizer.js +index f2dc516..65059e8 100644 +--- a/tools/js-optimizer.js ++++ b/tools/js-optimizer.js +@@ -1321,7 +1321,7 @@ function normalizeAsm(func) { + var name = v[0]; + var value = v[1]; + if (!(name in data.vars)) { +- assert(value[0] == 'num' || (value[0] == 'unary-prefix' && value[2][0] == 'num')); // must be valid coercion no-op ++ if (!(value[0] == 'num' || (value[0] == 'unary-prefix' && value[2][0] == 'num'))) break outer; // must be valid coercion no-op + data.vars[name] = detectAsmCoercion(value); + v.length = 1; // make an un-assigning var + } else { +@@ -1331,6 +1331,7 @@ function normalizeAsm(func) { + i++; + } + // finally, look for other var definitions and collect them ++ var extra = []; + while (i < stats.length) { + traverse(stats[i], function(node, type) { + if (type == 'var') { +@@ -1340,6 +1341,7 @@ function normalizeAsm(func) { + var value = v[1]; + if (!(name in data.vars)) { + data.vars[name] = detectAsmCoercion(value); ++ extra.push(['var', [[name]]]); // add a 'var' for normal JS + } + } + unVarify(node[1], node); +@@ -1353,6 +1355,7 @@ function normalizeAsm(func) { + }); + i++; + } ++ if (extra.length > 0) stats.splice.apply(stats, [0, 0].concat(extra)); + //printErr('normalized \n\n' + astToSrc(func) + '\n\nwith: ' + JSON.stringify(data)); + return data; + } +diff --git a/tools/test-js-optimizer-asm-regs-output.js b/tools/test-js-optimizer-asm-regs-output.js +index 99bccd2..f84b8d5 100644 +--- a/tools/test-js-optimizer-asm-regs-output.js ++++ b/tools/test-js-optimizer-asm-regs-output.js +@@ -9,6 +9,18 @@ function asm(d1, i2) { + d4 = d1 * 5; + return d4; + } ++function asm2(d1, i2) { ++ d1 = +d1; ++ i2 = i2 | 0; ++ var i3 = 0, d4 = +0; ++ i3 = i2; ++ i2 = d1 + i3 | 0; ++ d1 = d(Math_max(10, Math_min(5, f()))); ++ i3 = i2 + 2 | 0; ++ print(i3); ++ d4 = d1 * 5; ++ return d4; ++} + function _doit(i1, i2, i3) { + i1 = i1 | 0; + i2 = i2 | 0; +diff --git a/tools/test-js-optimizer-asm-regs.js b/tools/test-js-optimizer-asm-regs.js +index 0afced2..fbaa7c4 100644 +--- a/tools/test-js-optimizer-asm-regs.js ++++ b/tools/test-js-optimizer-asm-regs.js +@@ -10,6 +10,19 @@ function asm(x, y) { + double2 = double1*5; + return double2; + } ++function asm2(x, y) { ++ x = +x; ++ y = y | 0; ++ var int1 = 0, int2 = 0; // do not mix the types! ++ var double1 = +0, double2 = +0; ++ var tempy = y; ++ int1 = (x+tempy)|0; ++ double1 = d(Math.max(10, Math_min(5, f()))); ++ int2 = (int1+2)|0; ++ print(int2); ++ double2 = double1*5; ++ return double2; ++} + function _doit($x, $y$0, $y$1) { + $x = $x | 0; + $y$0 = $y$0 | 0; +@@ -41,5 +54,5 @@ function retf() { + } + // missing final return, need it as a float + } +-// EMSCRIPTEN_GENERATED_FUNCTIONS: ["asm", "_doit", "rett", "ret2t", "retf"] ++// EMSCRIPTEN_GENERATED_FUNCTIONS: ["asm", "asm2", "_doit", "rett", "ret2t", "retf"] + diff --git a/src/intertyper.js b/src/intertyper.js index c1a98354..2103ecfa 100644 --- a/src/intertyper.js +++ b/src/intertyper.js @@ -677,7 +677,7 @@ function intertyper(data, sidePass, baseLineNums) { item.type = item.tokens[1].text; Types.needAnalysis[item.type] = 0; while (['@', '%'].indexOf(item.tokens[2].text[0]) == -1 && !(item.tokens[2].text in PARSABLE_LLVM_FUNCTIONS) && - item.tokens[2].text != 'null' && item.tokens[2].text != 'asm') { + item.tokens[2].text != 'null' && item.tokens[2].text != 'asm' && item.tokens[2].text != 'undef') { assert(item.tokens[2].text != 'asm', 'Inline assembly cannot be compiled to JavaScript!'); item.tokens.splice(2, 1); } @@ -741,10 +741,12 @@ function intertyper(data, sidePass, baseLineNums) { processItem: function(item) { item.intertype = 'atomic'; if (item.tokens[0].text == 'atomicrmw') { + if (item.tokens[1].text == 'volatile') item.tokens.splice(1, 1); item.op = item.tokens[1].text; item.tokens.splice(1, 1); } else { assert(item.tokens[0].text == 'cmpxchg') + if (item.tokens[1].text == 'volatile') item.tokens.splice(1, 1); item.op = 'cmpxchg'; } var last = getTokenIndexByText(item.tokens, ';'); diff --git a/src/jsifier.js b/src/jsifier.js index 761a5fec..ff58ece2 100644 --- a/src/jsifier.js +++ b/src/jsifier.js @@ -375,6 +375,7 @@ function JSify(data, functionsOnly, givenFunctions) { var ret = [item]; item.JS = 'var ' + item.ident + ';'; // Set the actual value in a postset, since it may be a global variable. We also order by dependencies there + Variables.globals[item.ident].targetIdent = item.value.ident; var value = Variables.globals[item.ident].resolvedAlias = finalizeLLVMParameter(item.value); var fix = ''; if (BUILD_AS_SHARED_LIB == 2 && !item.private_) { @@ -478,8 +479,7 @@ function JSify(data, functionsOnly, givenFunctions) { ident = '_' + ident; } var depsText = (deps ? '\n' + deps.map(addFromLibrary).filter(function(x) { return x != '' }).join('\n') : ''); - // redirected idents just need a var, but no value assigned to them - it would be unused - var contentText = isFunction ? snippet : ('var ' + ident + (redirectedIdent ? '' : '=' + snippet) + ';'); + var contentText = isFunction ? snippet : ('var ' + ident + '=' + snippet + ';'); if (ASM_JS) { var sig = LibraryManager.library[ident.substr(1) + '__sig']; if (isFunction && sig && LibraryManager.library[ident.substr(1) + '__asm']) { @@ -506,9 +506,9 @@ function JSify(data, functionsOnly, givenFunctions) { item.JS = ''; } else if (LibraryManager.library.hasOwnProperty(shortident)) { item.JS = addFromLibrary(shortident); - } else { + } else if (!LibraryManager.library.hasOwnProperty(shortident + '__inline')) { item.JS = 'var ' + item.ident + '; // stub for ' + item.ident; - if (WARN_ON_UNDEFINED_SYMBOLS) { + if (WARN_ON_UNDEFINED_SYMBOLS || ASM_JS) { // always warn on undefs in asm, since it breaks validation warn('Unresolved symbol: ' + item.ident); } } @@ -631,15 +631,6 @@ function JSify(data, functionsOnly, givenFunctions) { } } - if (PROFILE) { - func.JS += ' if (PROFILING) { ' - + 'var __parentProfilingNode__ = PROFILING_NODE; PROFILING_NODE = PROFILING_NODE.children["' + func.ident + '"]; ' - + 'if (!PROFILING_NODE) __parentProfilingNode__.children["' + func.ident + '"] = PROFILING_NODE = { time: 0, children: {}, calls: 0 };' - + 'PROFILING_NODE.calls++; ' - + 'var __profilingStartTime__ = Date.now() ' - + '}\n'; - } - if (true) { // TODO: optimize away when not needed if (CLOSURE_ANNOTATIONS) func.JS += '/** @type {number} */'; func.JS += ' var label = 0;\n'; @@ -923,7 +914,11 @@ function JSify(data, functionsOnly, givenFunctions) { case VAR_NATIVIZED: if (isNumber(item.ident)) { // Direct write to a memory address; this may be an intentional segfault, if not, it is a bug in the source - return 'throw "fault on write to ' + item.ident + '";'; + if (ASM_JS) { + return 'abort(' + item.ident + ')'; + } else { + return 'throw "fault on write to ' + item.ident + '";'; + } } return item.ident + '=' + value + ';'; // We have the actual value here break; @@ -1145,12 +1140,6 @@ function JSify(data, functionsOnly, givenFunctions) { }); makeFuncLineActor('return', function(item) { var ret = RuntimeGenerator.stackExit(item.funcData.initialStack, item.funcData.otherStackAllocations) + ';\n'; - if (PROFILE) { - ret += 'if (PROFILING) { ' - + 'PROFILING_NODE.time += Date.now() - __profilingStartTime__; ' - + 'PROFILING_NODE = __parentProfilingNode__ ' - + '}\n'; - } if (LABEL_DEBUG && functionNameFilterTest(item.funcData.ident)) { ret += "Module.print(INDENT + 'Exiting: " + item.funcData.ident + "');\n" + "INDENT = INDENT.substr(0, INDENT.length-2);\n"; @@ -1215,10 +1204,13 @@ function JSify(data, functionsOnly, givenFunctions) { switch (item.op) { case 'add': return '(tempValue=' + makeGetValue(param1, 0, type) + ',' + makeSetValue(param1, 0, 'tempValue+' + param2, type, null, null, null, null, ',') + ',tempValue)'; case 'sub': return '(tempValue=' + makeGetValue(param1, 0, type) + ',' + makeSetValue(param1, 0, 'tempValue-' + param2, type, null, null, null, null, ',') + ',tempValue)'; + case 'or': return '(tempValue=' + makeGetValue(param1, 0, type) + ',' + makeSetValue(param1, 0, 'tempValue|' + param2, type, null, null, null, null, ',') + ',tempValue)'; + case 'and': return '(tempValue=' + makeGetValue(param1, 0, type) + ',' + makeSetValue(param1, 0, 'tempValue&' + param2, type, null, null, null, null, ',') + ',tempValue)'; + case 'xor': return '(tempValue=' + makeGetValue(param1, 0, type) + ',' + makeSetValue(param1, 0, 'tempValue^' + param2, type, null, null, null, null, ',') + ',tempValue)'; case 'xchg': return '(tempValue=' + makeGetValue(param1, 0, type) + ',' + makeSetValue(param1, 0, param2, type, null, null, null, null, ',') + ',tempValue)'; case 'cmpxchg': { var param3 = finalizeLLVMParameter(item.params[2]); - return '(tempValue=' + makeGetValue(param1, 0, type) + ',(' + makeGetValue(param1, 0, type) + '==' + param2 + ' ? ' + makeSetValue(param1, 0, param3, type, null, null, null, null, ',') + ' : 0),tempValue)'; + return '(tempValue=' + makeGetValue(param1, 0, type) + ',(' + makeGetValue(param1, 0, type) + '==(' + param2 + '|0) ? ' + makeSetValue(param1, 0, param3, type, null, null, null, null, ',') + ' : 0),tempValue)'; } default: throw 'unhandled atomic op: ' + item.op; } @@ -1237,6 +1229,15 @@ function JSify(data, functionsOnly, givenFunctions) { var impl = item.ident ? getVarImpl(item.funcData, item.ident) : VAR_EMULATED; switch (impl) { case VAR_NATIVIZED: { + if (isNumber(item.ident)) { + item.assignTo = null; + // Direct read from a memory address; this may be an intentional segfault, if not, it is a bug in the source + if (ASM_JS) { + return 'abort(' + item.ident + ')'; + } else { + return 'throw "fault on read from ' + item.ident + '";'; + } + } return value; // We have the actual value here } case VAR_EMULATED: return makeGetValue(value, 0, item.type, 0, item.unsigned, 0, item.align); @@ -1260,7 +1261,7 @@ function JSify(data, functionsOnly, givenFunctions) { makeFuncLineActor('insertvalue', function(item) { assert(item.indexes.length == 1); // TODO: see extractvalue var ret = '(', ident; - if (item.ident === 'undef') { + if (item.ident === '0') { item.ident = 'tempValue'; ret += item.ident + ' = [' + makeEmptyStruct(item.type) + '], '; } @@ -1300,6 +1301,7 @@ function JSify(data, functionsOnly, givenFunctions) { // We cannot compile assembly. See comment in intertyper.js:'Call' assert(ident != 'asm', 'Inline assembly cannot be compiled to JavaScript!'); + ident = Variables.resolveAliasToIdent(ident); var shortident = ident.slice(1); var callIdent = LibraryManager.getRootIdent(shortident); if (callIdent) { @@ -1417,6 +1419,9 @@ function JSify(data, functionsOnly, givenFunctions) { if (ASM_JS) { assert(returnType.search(/\("'\[,/) == -1); // XXX need isFunctionType(type, out) callIdent = '(' + callIdent + ')&{{{ FTM_' + sig + ' }}}'; // the function table mask is set in emscripten.py + } else if (SAFE_DYNCALLS) { + assert(!ASM_JS, 'cannot emit safe dyncalls in asm'); + callIdent = '(tempInt=' + callIdent + ',tempInt < 0 || tempInt >= FUNCTION_TABLE.length-1 || !FUNCTION_TABLE[tempInt] ? abort("dyncall error: ' + sig + ' " + FUNCTION_TABLE_NAMES[tempInt]) : tempInt)'; } callIdent = Functions.getTable(sig) + '[' + callIdent + ']'; } @@ -1527,7 +1532,7 @@ function JSify(data, functionsOnly, givenFunctions) { print('// ASM_LIBRARY FUNCTIONS'); function fix(f) { // fix indenting to not confuse js optimizer f = f.substr(f.indexOf('f')); // remove initial spaces before 'function' - f = f.substr(0, f.lastIndexOf('\n')+1); // remove spaces and last } + f = f.substr(0, f.lastIndexOf('\n')+1); // remove spaces and last } XXX assumes function has multiple lines return f + '}'; // add unindented } to match function } print(asmLibraryFunctions.map(fix).join('\n')); @@ -1547,6 +1552,10 @@ function JSify(data, functionsOnly, givenFunctions) { // This is the main 'post' pass. Print out the generated code that we have here, together with the // rest of the output that we started to print out earlier (see comment on the // "Final shape that will be created"). + if (CORRUPTION_CHECK) { + assert(!ASM_JS); // cannot monkeypatch asm! + print(processMacros(read('corruptionCheck.js'))); + } if (PRECISE_I64_MATH && Types.preciseI64MathUsed) { print(read('long.js')); } else { @@ -1579,9 +1588,11 @@ function JSify(data, functionsOnly, givenFunctions) { var shellParts = read(shellFile).split('{{BODY}}'); print(shellParts[1]); // Print out some useful metadata (for additional optimizations later, like the eliminator) - print('// EMSCRIPTEN_GENERATED_FUNCTIONS: ' + JSON.stringify(keys(Functions.implementedFunctions).filter(function(func) { - return IGNORED_FUNCTIONS.indexOf(func.ident) < 0; - })) + '\n'); + if (EMIT_GENERATED_FUNCTIONS) { + print('// EMSCRIPTEN_GENERATED_FUNCTIONS: ' + JSON.stringify(keys(Functions.implementedFunctions).filter(function(func) { + return IGNORED_FUNCTIONS.indexOf(func.ident) < 0; + })) + '\n'); + } PassManager.serialize(); diff --git a/src/library.js b/src/library.js index 1e026937..d787e30c 100644 --- a/src/library.js +++ b/src/library.js @@ -624,7 +624,12 @@ LibraryManager.library = { // dirent.h // ========================================================================== - __dirent_struct_layout: Runtime.generateStructInfo(['d_ino', 'd_name', 'd_off', 'd_reclen', 'd_type'], '%struct.dirent'), + __dirent_struct_layout: Runtime.generateStructInfo([ + ['i32', 'd_ino'], + ['b1024', 'd_name'], + ['i32', 'd_off'], + ['i32', 'd_reclen'], + ['i32', 'd_type']]), opendir__deps: ['$FS', '__setErrNo', '$ERRNO_CODES', '__dirent_struct_layout'], opendir: function(dirname) { // DIR *opendir(const char *dirname); @@ -786,7 +791,9 @@ LibraryManager.library = { // utime.h // ========================================================================== - __utimbuf_struct_layout: Runtime.generateStructInfo(['actime', 'modtime'], '%struct.utimbuf'), + __utimbuf_struct_layout: Runtime.generateStructInfo([ + ['i32', 'actime'], + ['i32', 'modtime']]), utime__deps: ['$FS', '__setErrNo', '$ERRNO_CODES', '__utimbuf_struct_layout'], utime: function(path, times) { // int utime(const char *path, const struct utimbuf *times); @@ -877,23 +884,23 @@ LibraryManager.library = { // ========================================================================== __stat_struct_layout: Runtime.generateStructInfo([ - 'st_dev', - 'st_ino', - 'st_mode', - 'st_nlink', - 'st_uid', - 'st_gid', - 'st_rdev', - 'st_size', - 'st_atime', - 'st_spare1', - 'st_mtime', - 'st_spare2', - 'st_ctime', - 'st_spare3', - 'st_blksize', - 'st_blocks', - 'st_spare4'], '%struct.stat'), + ['i32', 'st_dev'], + ['i32', 'st_ino'], + ['i32', 'st_mode'], + ['i32', 'st_nlink'], + ['i32', 'st_uid'], + ['i32', 'st_gid'], + ['i32', 'st_rdev'], + ['i32', 'st_size'], + ['i32', 'st_atime'], + ['i32', 'st_spare1'], + ['i32', 'st_mtime'], + ['i32', 'st_spare2'], + ['i32', 'st_ctime'], + ['i32', 'st_spare3'], + ['i32', 'st_blksize'], + ['i32', 'st_blocks'], + ['i32', 'st_spare4']]), stat__deps: ['$FS', '__stat_struct_layout'], stat: function(path, buf, dontResolveLastLink) { // http://pubs.opengroup.org/onlinepubs/7908799/xsh/stat.html @@ -1065,17 +1072,17 @@ LibraryManager.library = { // ========================================================================== __statvfs_struct_layout: Runtime.generateStructInfo([ - 'f_bsize', - 'f_frsize', - 'f_blocks', - 'f_bfree', - 'f_bavail', - 'f_files', - 'f_ffree', - 'f_favail', - 'f_fsid', - 'f_flag', - 'f_namemax'], '%struct.statvfs'), + ['i32', 'f_bsize'], + ['i32', 'f_frsize'], + ['i32', 'f_blocks'], + ['i32', 'f_bfree'], + ['i32', 'f_bavail'], + ['i32', 'f_files'], + ['i32', 'f_ffree'], + ['i32', 'f_favail'], + ['i32', 'f_fsid'], + ['i32', 'f_flag'], + ['i32', 'f_namemax']]), statvfs__deps: ['$FS', '__statvfs_struct_layout'], statvfs: function(path, buf) { // http://pubs.opengroup.org/onlinepubs/7908799/xsh/stat.html @@ -1110,12 +1117,12 @@ LibraryManager.library = { // ========================================================================== __flock_struct_layout: Runtime.generateStructInfo([ - 'l_type', - 'l_whence', - 'l_start', - 'l_len', - 'l_pid', - 'l_xxx'], '%struct.flock'), + ['i16', 'l_type'], + ['i16', 'l_whence'], + ['i32', 'l_start'], + ['i32', 'l_len'], + ['i16', 'l_pid'], + ['i16', 'l_xxx']]), open__deps: ['$FS', '__setErrNo', '$ERRNO_CODES', '__dirent_struct_layout'], open: function(path, oflag, varargs) { // int open(const char *path, int oflag, ...); @@ -1336,7 +1343,10 @@ LibraryManager.library = { // poll.h // ========================================================================== - __pollfd_struct_layout: Runtime.generateStructInfo(['fd', 'events', 'revents'], '%struct.pollfd'), + __pollfd_struct_layout: Runtime.generateStructInfo([ + ['i32', 'fd'], + ['i16', 'events'], + ['i16', 'revents']]), poll__deps: ['$FS', '__pollfd_struct_layout'], poll: function(fds, nfds, timeout) { // int poll(struct pollfd fds[], nfds_t nfds, int timeout); @@ -2393,6 +2403,7 @@ LibraryManager.library = { case {{{ cDefine('_SC_STREAM_MAX') }}}: return 16; case {{{ cDefine('_SC_TZNAME_MAX') }}}: return 6; case {{{ cDefine('_SC_THREAD_DESTRUCTOR_ITERATIONS') }}}: return 4; + case {{{ cDefine('_SC_NPROCESSORS_ONLN') }}}: return 1; } ___setErrNo(ERRNO_CODES.EINVAL); return -1; @@ -2481,6 +2492,17 @@ LibraryManager.library = { continue; } + // TODO: Support strings like "%5c" etc. + if (format[formatIndex] === '%' && format[formatIndex+1] == 'c') { + var argPtr = {{{ makeGetValue('varargs', 'argIndex', 'void*') }}}; + argIndex += Runtime.getNativeFieldSize('void*'); + fields++; + next = get(); + {{{ makeSetValue('argPtr', 0, 'next', 'i8') }}} + formatIndex += 2; + continue; + } + // remove whitespace while (1) { next = get(); @@ -3342,14 +3364,15 @@ LibraryManager.library = { ___setErrNo(ERRNO_CODES.ECHILD); return -1; }, - perror__deps: ['puts', 'putc', 'strerror', '__errno_location'], + perror__deps: ['puts', 'fputs', 'fputc', 'strerror', '__errno_location'], perror: function(s) { // void perror(const char *s); // http://pubs.opengroup.org/onlinepubs/000095399/functions/perror.html + var stdout = {{{ makeGetValue(makeGlobalUse('_stdout'), '0', 'void*') }}}; if (s) { - _puts(s); - _putc(':'.charCodeAt(0)); - _putc(' '.charCodeAt(0)); + _fputs(s, stdout); + _fputc(':'.charCodeAt(0), stdout); + _fputc(' '.charCodeAt(0), stdout); } var errnum = {{{ makeGetValue('___errno_location()', '0', 'i32') }}}; _puts(_strerror(errnum)); @@ -3647,6 +3670,17 @@ LibraryManager.library = { abs: 'Math.abs', labs: 'Math.abs', +#if USE_TYPED_ARRAYS == 2 + llabs__deps: [function() { Types.preciseI64MathUsed = 1 }], + llabs: function(lo, hi) { + i64Math.abs(lo, hi); + {{{ makeStructuralReturn([makeGetTempDouble(0, 'i32'), makeGetTempDouble(1, 'i32')]) }}}; + }, +#else + llabs: function(lo, hi) { + throw 'unsupported llabs'; + }, +#endif exit__deps: ['_exit'], exit: function(status) { @@ -3704,93 +3738,6 @@ LibraryManager.library = { return ret; }, - strtod__deps: ['isspace', 'isdigit'], - strtod: function(str, endptr) { - var origin = str; - - // Skip space. - while (_isspace({{{ makeGetValue('str', 0, 'i8') }}})) str++; - - // Check for a plus/minus sign. - var multiplier = 1; - if ({{{ makeGetValue('str', 0, 'i8') }}} == '-'.charCodeAt(0)) { - multiplier = -1; - str++; - } else if ({{{ makeGetValue('str', 0, 'i8') }}} == '+'.charCodeAt(0)) { - str++; - } - - var chr; - var ret = 0; - - // Get whole part. - var whole = false; - while(1) { - chr = {{{ makeGetValue('str', 0, 'i8') }}}; - if (!_isdigit(chr)) break; - whole = true; - ret = ret*10 + chr - '0'.charCodeAt(0); - str++; - } - - // Get fractional part. - var fraction = false; - if ({{{ makeGetValue('str', 0, 'i8') }}} == '.'.charCodeAt(0)) { - str++; - var mul = 1/10; - while(1) { - chr = {{{ makeGetValue('str', 0, 'i8') }}}; - if (!_isdigit(chr)) break; - fraction = true; - ret += mul*(chr - '0'.charCodeAt(0)); - mul /= 10; - str++; - } - } - - if (!whole && !fraction) { - if (endptr) { - {{{ makeSetValue('endptr', 0, 'origin', '*') }}} - } - return 0; - } - - // Get exponent part. - chr = {{{ makeGetValue('str', 0, 'i8') }}}; - if (chr == 'e'.charCodeAt(0) || chr == 'E'.charCodeAt(0)) { - str++; - var exponent = 0; - var expNegative = false; - chr = {{{ makeGetValue('str', 0, 'i8') }}}; - if (chr == '-'.charCodeAt(0)) { - expNegative = true; - str++; - } else if (chr == '+'.charCodeAt(0)) { - str++; - } - chr = {{{ makeGetValue('str', 0, 'i8') }}}; - while(1) { - if (!_isdigit(chr)) break; - exponent = exponent*10 + chr - '0'.charCodeAt(0); - str++; - chr = {{{ makeGetValue('str', 0, 'i8') }}}; - } - if (expNegative) exponent = -exponent; - ret *= Math.pow(10, exponent); - } - - // Set end pointer. - if (endptr) { - {{{ makeSetValue('endptr', 0, 'str', '*') }}} - } - - return ret * multiplier; - }, - strtod_l: 'strtod', // no locale support yet - strtold: 'strtod', // XXX add real support for long double - strtold_l: 'strtold', // no locale support yet - strtof: 'strtod', // use stdtod to handle strtof - _parseInt__deps: ['isspace', '__setErrNo', '$ERRNO_CODES'], _parseInt: function(str, endptr, base, min, max, bits, unsign) { // Skip space. @@ -3949,11 +3896,6 @@ LibraryManager.library = { }, strtoull_l: 'strtoull', // no locale support yet - atof__deps: ['strtod'], - atof: function(ptr) { - return _strtod(ptr, null); - }, - atoi__deps: ['strtol'], atoi: function(ptr) { return _strtol(ptr, null, 10); @@ -4202,6 +4144,8 @@ LibraryManager.library = { return 1; }, + arc4random: 'rand', + // ========================================================================== // string.h // ========================================================================== @@ -4290,8 +4234,9 @@ LibraryManager.library = { ptr = ptr|0; value = value|0; num = num|0; var stop = 0, value4 = 0, stop4 = 0, unaligned = 0; stop = (ptr + num)|0; - if (num|0 >= {{{ SEEK_OPTIMAL_ALIGN_MIN }}}) { + if ((num|0) >= {{{ Math.round(2.5*UNROLL_LOOP_MAX) }}}) { // This is unaligned, but quite large, so work hard to get to aligned settings + value = value & 0xff; unaligned = ptr & 3; value4 = value | (value << 8) | (value << 16) | (value << 24); stop4 = stop & ~3; @@ -4502,11 +4447,16 @@ LibraryManager.library = { return 0; }, + memcmp__asm: 'true', + memcmp__sig: 'iiii', memcmp: function(p1, p2, num) { - for (var i = 0; i < num; i++) { - var v1 = {{{ makeGetValue('p1', 'i', 'i8', 0, 1) }}}; - var v2 = {{{ makeGetValue('p2', 'i', 'i8', 0, 1) }}}; - if (v1 != v2) return v1 > v2 ? 1 : -1; + p1 = p1|0; p2 = p2|0; num = num|0; + var i = 0, v1 = 0, v2 = 0; + while ((i|0) < (num|0)) { + var v1 = {{{ makeGetValueAsm('p1', 'i', 'i8', true) }}}; + var v2 = {{{ makeGetValueAsm('p2', 'i', 'i8', true) }}}; + if ((v1|0) != (v2|0)) return ((v1|0) > (v2|0) ? 1 : -1)|0; + i = (i+1)|0; } return 0; }, @@ -4607,10 +4557,8 @@ LibraryManager.library = { __strtok_state: 0, strtok__deps: ['__strtok_state', 'strtok_r'], + strtok__postset: '___strtok_state = Runtime.staticAlloc(4);', strtok: function(s, delim) { - if (!___strtok_state) { - ___strtok_state = _malloc(4); - } return _strtok_r(s, delim, ___strtok_state); }, @@ -4948,6 +4896,20 @@ LibraryManager.library = { #endif }, + llvm_ctpop_i32: function(x) { + var ret = 0; + while (x) { + if (x&1) ret++; + x >>= 1; + } + return ret; + }, + + llvm_ctpop_i64__deps: ['llvm_ctpop_i32'], + llvm_ctpop_i64: function(l, h) { + return _llvm_ctpop_i32(l) + _llvm_ctpop_i32(h); + }, + llvm_trap: function() { throw 'trap! ' + new Error().stack; }, @@ -5070,6 +5032,7 @@ LibraryManager.library = { }, __cxa_call_unexpected: function(exception) { + Module.printErr('Unexpected exception thrown, this is not properly supported - aborting'); ABORT = true; throw exception; }, @@ -5477,6 +5440,14 @@ LibraryManager.library = { return -a; }, copysignf: 'copysign', + __signbit__deps: ['copysign'], + __signbit: function(x) { + // We implement using copysign so that we get support + // for negative zero (once copysign supports that). + return _copysign(1.0, x) < 0; + }, + __signbitf: '__signbit', + __signbitd: '__signbit', hypot: function(a, b) { return Math.sqrt(a*a + b*b); }, @@ -5619,11 +5590,11 @@ LibraryManager.library = { // ========================================================================== __utsname_struct_layout: Runtime.generateStructInfo([ - 'sysname', - 'nodename', - 'release', - 'version', - 'machine'], '%struct.utsname'), + ['b32', 'sysname'], + ['b32', 'nodename'], + ['b32', 'release'], + ['b32', 'version'], + ['b32', 'machine']]), uname__deps: ['__utsname_struct_layout'], uname: function(name) { // int uname(struct utsname *name); @@ -5823,17 +5794,17 @@ LibraryManager.library = { }, __tm_struct_layout: Runtime.generateStructInfo([ - 'tm_sec', - 'tm_min', - 'tm_hour', - 'tm_mday', - 'tm_mon', - 'tm_year', - 'tm_wday', - 'tm_yday', - 'tm_isdst', - 'tm_gmtoff', - 'tm_zone'], '%struct.tm'), + ['i32', 'tm_sec'], + ['i32', 'tm_min'], + ['i32', 'tm_hour'], + ['i32', 'tm_mday'], + ['i32', 'tm_mon'], + ['i32', 'tm_year'], + ['i32', 'tm_wday'], + ['i32', 'tm_yday'], + ['i32', 'tm_isdst'], + ['i32', 'tm_gmtoff'], + ['i32', 'tm_zone']]), // Statically allocated time struct. __tm_current: 'allocate({{{ Runtime.QUANTUM_SIZE }}}*26, "i8", ALLOC_STACK)', // Statically allocated timezone strings. @@ -6032,7 +6003,18 @@ LibraryManager.library = { // sys/time.h // ========================================================================== - __timespec_struct_layout: Runtime.generateStructInfo(['tv_sec', 'tv_nsec'], '%struct.timespec'), + __timespec_struct_layout: Runtime.generateStructInfo([ + ['i32', 'tv_sec'], + ['i32', 'tv_nsec']]), + nanosleep__deps: ['usleep', '__timespec_struct_layout'], + nanosleep: function(rqtp, rmtp) { + // int nanosleep(const struct timespec *rqtp, struct timespec *rmtp); + var seconds = {{{ makeGetValue('rqtp', '___timespec_struct_layout.tv_sec', 'i32') }}}; + var nanoseconds = {{{ makeGetValue('rqtp', '___timespec_struct_layout.tv_nsec', 'i32') }}}; + {{{ makeSetValue('rmtp', '___timespec_struct_layout.tv_sec', '0', 'i32') }}} + {{{ makeSetValue('rmtp', '___timespec_struct_layout.tv_nsec', '0', 'i32') }}} + return _usleep((seconds * 1e6) + (nanoseconds / 1000)); + }, // TODO: Implement these for real. clock_gettime__deps: ['__timespec_struct_layout'], clock_gettime: function(clk_id, tp) { @@ -6089,10 +6071,10 @@ LibraryManager.library = { // ========================================================================== __tms_struct_layout: Runtime.generateStructInfo([ - 'tms_utime', - 'tms_stime', - 'tms_cutime', - 'tms_cstime'], '%struct.tms'), + ['i32', 'tms_utime'], + ['i32', 'tms_stime'], + ['i32', 'tms_cutime'], + ['i32', 'tms_cstime']]), times__deps: ['__tms_struct_layout', 'memset'], times: function(buffer) { // clock_t times(struct tms *buffer); @@ -6139,7 +6121,7 @@ LibraryManager.library = { setjmp__inline: function(env) { // Save the label - return '(tempInt = setjmpId++, mySetjmpIds[tempInt] = 1, setjmpLabels[tempInt] = label,' + makeSetValue(env, '0', 'tempInt', 'i32') + ', 0)'; + return '(tempInt = setjmpId++, mySetjmpIds[tempInt] = 1, setjmpLabels[tempInt] = label,' + makeSetValue(env, '0', 'tempInt', 'i32', undefined, undefined, undefined, undefined, ',') + ', 0)'; }, longjmp: function(env, value) { @@ -6614,7 +6596,9 @@ LibraryManager.library = { // ========================================================================== // TODO: Implement for real. - __rlimit_struct_layout: Runtime.generateStructInfo(['rlim_cur', 'rlim_max'], '%struct.rlimit'), + __rlimit_struct_layout: Runtime.generateStructInfo([ + ['i32', 'rlim_cur'], + ['i32', 'rlim_max']]), getrlimit__deps: ['__rlimit_struct_layout'], getrlimit: function(resource, rlp) { // int getrlimit(int resource, struct rlimit *rlp); @@ -6630,22 +6614,22 @@ LibraryManager.library = { // TODO: Implement for real. We just do time used, and no useful data __rusage_struct_layout: Runtime.generateStructInfo([ - 'ru_utime', - 'ru_stime', - 'ru_maxrss', - 'ru_ixrss', - 'ru_idrss', - 'ru_isrss', - 'ru_minflt', - 'ru_majflt', - 'ru_nswap', - 'ru_inblock', - 'ru_oublock', - 'ru_msgsnd', - 'ru_msgrcv', - 'ru_nsignals', - 'ru_nvcsw', - 'ru_nivcsw'], '%struct.rusage'), + ['i64', 'ru_utime'], + ['i64', 'ru_stime'], + ['i32', 'ru_maxrss'], + ['i32', 'ru_ixrss'], + ['i32', 'ru_idrss'], + ['i32', 'ru_isrss'], + ['i32', 'ru_minflt'], + ['i32', 'ru_majflt'], + ['i32', 'ru_nswap'], + ['i32', 'ru_inblock'], + ['i32', 'ru_oublock'], + ['i32', 'ru_msgsnd'], + ['i32', 'ru_msgrcv'], + ['i32', 'ru_nsignals'], + ['i32', 'ru_nvcsw'], + ['i32', 'ru_nivcsw']]), getrusage__deps: ['__rusage_struct_layout'], getrusage: function(resource, rlp) { // %struct.timeval = type { i32, i32 } @@ -6660,6 +6644,13 @@ LibraryManager.library = { }, // ========================================================================== + // sched.h (stubs only - no thread support yet!) + // ========================================================================== + sched_yield: function() { + return 0; + }, + + // ========================================================================== // pthread.h (stubs for mutexes only - no thread support yet!) // ========================================================================== @@ -6670,10 +6661,20 @@ LibraryManager.library = { pthread_mutexattr_destroy: function() {}, pthread_mutex_lock: function() {}, pthread_mutex_unlock: function() {}, + pthread_mutex_trylock: function() { + return 0; + }, pthread_cond_init: function() {}, pthread_cond_destroy: function() {}, - pthread_cond_broadcast: function() {}, - pthread_cond_wait: function() {}, + pthread_cond_broadcast: function() { + return 0; + }, + pthread_cond_wait: function() { + return 0; + }, + pthread_cond_timedwait: function() { + return 0; + }, pthread_self: function() { //FIXME: assumes only a single thread return 0; @@ -6712,17 +6713,27 @@ LibraryManager.library = { pthread_key_create: function(key, destructor) { if (!_pthread_key_create.keys) _pthread_key_create.keys = {}; - _pthread_key_create.keys[key] = null; + // values start at 0 + _pthread_key_create.keys[key] = 0; }, pthread_getspecific: function(key) { - return _pthread_key_create.keys[key]; + return _pthread_key_create.keys[key] || 0; }, pthread_setspecific: function(key, value) { _pthread_key_create.keys[key] = value; }, + pthread_key_delete: ['$ERRNO_CODES'], + pthread_key_delete: function(key) { + if (_pthread_key_create.keys[key]) { + delete _pthread_key_create.keys[key]; + return 0; + } + return ERRNO_CODES.EINVAL; + }, + pthread_cleanup_push: function(routine, arg) { __ATEXIT__.push({ func: function() { Runtime.dynCall('vi', routine, [arg]) } }) _pthread_cleanup_push.level = __ATEXIT__.length; @@ -7227,23 +7238,56 @@ LibraryManager.library = { }, select: function(nfds, readfds, writefds, exceptfds, timeout) { - // only readfds are supported, not writefds or exceptfds + // readfds are supported, + // writefds checks socket open status + // exceptfds not supported // timeout is always 0 - fully async - assert(!writefds && !exceptfds); - var ret = 0; - var l = {{{ makeGetValue('readfds', 0, 'i32') }}}; - var h = {{{ makeGetValue('readfds', 4, 'i32') }}}; - nfds = Math.min(64, nfds); // fd sets have 64 bits - for (var fd = 0; fd < nfds; fd++) { - var bit = fd % 32, int = fd < 32 ? l : h; - if (int & (1 << bit)) { - // index is in the set, check if it is ready for read - var info = Sockets.fds[fd]; - if (!info) continue; - if (info.hasData()) ret++; + assert(!exceptfds); + + function canRead(info) { + // make sure hasData exists. + // we do create it when the socket is connected, + // but other implementations may create it lazily + return info.hasData && info.hasData(); + } + + function canWrite(info) { + // make sure socket exists. + // we do create it when the socket is connected, + // but other implementations may create it lazily + return info.socket && (info.socket.readyState == info.socket.OPEN); + } + + function checkfds(nfds, fds, can) { + if (!fds) return 0; + + var bitsSet = 0; + var dstLow = 0; + var dstHigh = 0; + var srcLow = {{{ makeGetValue('fds', 0, 'i32') }}}; + var srcHigh = {{{ makeGetValue('fds', 4, 'i32') }}}; + nfds = Math.min(64, nfds); // fd sets have 64 bits + + for (var fd = 0; fd < nfds; fd++) { + var mask = 1 << (fd % 32), int = fd < 32 ? srcLow : srcHigh; + if (int & mask) { + // index is in the set, check if it is ready for read + var info = Sockets.fds[fd]; + if (info && can(info)) { + // set bit + fd < 32 ? (dstLow = dstLow | mask) : (dstHigh = dstHigh | mask); + bitsSet++; + } + } } + + {{{ makeSetValue('fds', 0, 'dstLow', 'i32') }}}; + {{{ makeSetValue('fds', 4, 'dstHigh', 'i32') }}}; + return bitsSet; } - return ret; + + return checkfds(nfds, readfds, canRead) + + checkfds(nfds, writefds, canWrite); }, // pty.h @@ -7290,35 +7334,22 @@ LibraryManager.library = { return Math.random(); }, - $Profiling: { - max_: 0, - times: null, - invalid: 0, - dump: function() { - if (Profiling.invalid) { - Module.printErr('Invalid # of calls to Profiling begin and end!'); - return; - } - Module.printErr('Profiling data:') - for (var i = 0; i < Profiling.max_; i++) { - Module.printErr('Block ' + i + ': ' + Profiling.times[i]); - } + emscripten_jcache_printf___deps: ['_formatString'], + emscripten_jcache_printf_: function(varargs) { + var MAX = 10240; + if (!_emscripten_jcache_printf_.buffer) { + _emscripten_jcache_printf_.buffer = _malloc(MAX); } + var i = 0; + do { + var curr = {{{ makeGetValue('varargs', 'i*4', 'i8') }}}; + {{{ makeSetValue('_emscripten_jcache_printf_.buffer', 'i', 'curr', 'i8') }}}; + i++; + assert(i*4 < MAX); + } while (curr != 0); + Module.print(intArrayToString(__formatString(_emscripten_jcache_printf_.buffer, varargs + i*4)).replace('\\n', '')); + Runtime.stackAlloc(-4*i); // free up the stack space we know is ok to free }, - EMSCRIPTEN_PROFILE_INIT__deps: ['$Profiling'], - EMSCRIPTEN_PROFILE_INIT: function(max_) { - Profiling.max_ = max_; - Profiling.times = new Array(max_); - for (var i = 0; i < max_; i++) Profiling.times[i] = 0; - }, - EMSCRIPTEN_PROFILE_BEGIN__inline: function(id) { - return 'Profiling.times[' + id + '] -= Date.now();' - + 'Profiling.invalid++;' - }, - EMSCRIPTEN_PROFILE_END__inline: function(id) { - return 'Profiling.times[' + id + '] += Date.now();' - + 'Profiling.invalid--;' - } }; function autoAddDeps(object, name) { diff --git a/src/library_browser.js b/src/library_browser.js index d16fbc0b..bdd94bac 100644 --- a/src/library_browser.js +++ b/src/library_browser.js @@ -3,7 +3,7 @@ // Utilities for browser environments mergeInto(LibraryManager.library, { - $Browser__postset: 'Module["requestFullScreen"] = function() { Browser.requestFullScreen() };\n' + // exports + $Browser__postset: 'Module["requestFullScreen"] = function(lockPointer, resizeCanvas) { Browser.requestFullScreen(lockPointer, resizeCanvas) };\n' + // exports 'Module["requestAnimationFrame"] = function(func) { Browser.requestAnimationFrame(func) };\n' + 'Module["pauseMainLoop"] = function() { Browser.mainLoop.pause() };\n' + 'Module["resumeMainLoop"] = function() { Browser.mainLoop.resume() };\n', @@ -40,6 +40,7 @@ mergeInto(LibraryManager.library, { } } }, + isFullScreen: false, pointerLock: false, moduleContextCreatedCallbacks: [], workers: [], @@ -68,6 +69,7 @@ mergeInto(LibraryManager.library, { function getMimetype(name) { return { 'jpg': 'image/jpeg', + 'jpeg': 'image/jpeg', 'png': 'image/png', 'bmp': 'image/bmp', 'ogg': 'audio/ogg', @@ -81,7 +83,7 @@ mergeInto(LibraryManager.library, { var imagePlugin = {}; imagePlugin['canHandle'] = function(name) { - return name.substr(-4) in { '.jpg': 1, '.png': 1, '.bmp': 1 }; + return !Module.noImageDecoding && /\.(jpg|jpeg|png|bmp)$/.exec(name); }; imagePlugin['handle'] = function(byteArray, name, onload, onerror) { var b = null; @@ -123,7 +125,7 @@ mergeInto(LibraryManager.library, { var audioPlugin = {}; audioPlugin['canHandle'] = function(name) { - return name.substr(-4) in { '.ogg': 1, '.wav': 1, '.mp3': 1 }; + return !Module.noAudioDecoding && name.substr(-4) in { '.ogg': 1, '.wav': 1, '.mp3': 1 }; }; audioPlugin['handle'] = function(byteArray, name, onload, onerror) { var done = false; @@ -200,8 +202,18 @@ mergeInto(LibraryManager.library, { return null; } #endif + var ctx; try { - var ctx = canvas.getContext(useWebGL ? 'experimental-webgl' : '2d'); + if (useWebGL) { + ctx = canvas.getContext('experimental-webgl', { +#if GL_TESTING + preserveDrawingBuffer: true, +#endif + alpha: false + }); + } else { + ctx = canvas.getContext('2d'); + } if (!ctx) throw ':('; } catch (e) { Module.print('Could not create canvas - ' + e); @@ -263,35 +275,59 @@ mergeInto(LibraryManager.library, { return ctx; }, - requestFullScreen: function() { + destroyContext: function(canvas, useWebGL, setInModule) {}, + + fullScreenHandlersInstalled: false, + lockPointer: undefined, + resizeCanvas: undefined, + requestFullScreen: function(lockPointer, resizeCanvas) { + this.lockPointer = lockPointer; + this.resizeCanvas = resizeCanvas; + if (typeof this.lockPointer === 'undefined') this.lockPointer = true; + if (typeof this.resizeCanvas === 'undefined') this.resizeCanvas = false; + var canvas = Module['canvas']; function fullScreenChange() { - var isFullScreen = false; + Browser.isFullScreen = false; if ((document['webkitFullScreenElement'] || document['webkitFullscreenElement'] || document['mozFullScreenElement'] || document['mozFullscreenElement'] || document['fullScreenElement'] || document['fullscreenElement']) === canvas) { canvas.requestPointerLock = canvas['requestPointerLock'] || canvas['mozRequestPointerLock'] || canvas['webkitRequestPointerLock']; - canvas.requestPointerLock(); - isFullScreen = true; + canvas.exitPointerLock = document['exitPointerLock'] || + document['mozExitPointerLock'] || + document['webkitExitPointerLock']; + canvas.exitPointerLock = canvas.exitPointerLock.bind(document); + canvas.cancelFullScreen = document['cancelFullScreen'] || + document['mozCancelFullScreen'] || + document['webkitCancelFullScreen']; + canvas.cancelFullScreen = canvas.cancelFullScreen.bind(document); + if (Browser.lockPointer) canvas.requestPointerLock(); + Browser.isFullScreen = true; + if (Browser.resizeCanvas) Browser.setFullScreenCanvasSize(); + } else if (Browser.resizeCanvas){ + Browser.setWindowedCanvasSize(); } - if (Module['onFullScreen']) Module['onFullScreen'](isFullScreen); + if (Module['onFullScreen']) Module['onFullScreen'](Browser.isFullScreen); } - document.addEventListener('fullscreenchange', fullScreenChange, false); - document.addEventListener('mozfullscreenchange', fullScreenChange, false); - document.addEventListener('webkitfullscreenchange', fullScreenChange, false); - function pointerLockChange() { Browser.pointerLock = document['pointerLockElement'] === canvas || document['mozPointerLockElement'] === canvas || document['webkitPointerLockElement'] === canvas; } - document.addEventListener('pointerlockchange', pointerLockChange, false); - document.addEventListener('mozpointerlockchange', pointerLockChange, false); - document.addEventListener('webkitpointerlockchange', pointerLockChange, false); + if (!this.fullScreenHandlersInstalled) { + this.fullScreenHandlersInstalled = true; + document.addEventListener('fullscreenchange', fullScreenChange, false); + document.addEventListener('mozfullscreenchange', fullScreenChange, false); + document.addEventListener('webkitfullscreenchange', fullScreenChange, false); + + document.addEventListener('pointerlockchange', pointerLockChange, false); + document.addEventListener('mozpointerlockchange', pointerLockChange, false); + document.addEventListener('webkitpointerlockchange', pointerLockChange, false); + } canvas.requestFullScreen = canvas['requestFullScreen'] || canvas['mozRequestFullScreen'] || @@ -369,7 +405,32 @@ mergeInto(LibraryManager.library, { canvas.width = width; canvas.height = height; if (!noUpdates) Browser.updateResizeListeners(); + }, + + windowedWidth: 0, + windowedHeight: 0, + setFullScreenCanvasSize: function() { + var canvas = Module['canvas']; + this.windowedWidth = canvas.width; + this.windowedHeight = canvas.height; + canvas.width = screen.width; + canvas.height = screen.height; + var flags = {{{ makeGetValue('SDL.screen+Runtime.QUANTUM_SIZE*0', '0', 'i32', 0, 1) }}}; + flags = flags | 0x00800000; // set SDL_FULLSCREEN flag + {{{ makeSetValue('SDL.screen+Runtime.QUANTUM_SIZE*0', '0', 'flags', 'i32') }}} + Browser.updateResizeListeners(); + }, + + setWindowedCanvasSize: function() { + var canvas = Module['canvas']; + canvas.width = this.windowedWidth; + canvas.height = this.windowedHeight; + var flags = {{{ makeGetValue('SDL.screen+Runtime.QUANTUM_SIZE*0', '0', 'i32', 0, 1) }}}; + flags = flags & ~0x00800000; // clear SDL_FULLSCREEN flag + {{{ makeSetValue('SDL.screen+Runtime.QUANTUM_SIZE*0', '0', 'flags', 'i32') }}} + Browser.updateResizeListeners(); } + }, emscripten_async_wget: function(url, file, onload, onerror) { diff --git a/src/library_egl.js b/src/library_egl.js index a9eb37dd..271ea29e 100644 --- a/src/library_egl.js +++ b/src/library_egl.js @@ -83,6 +83,17 @@ var LibraryEGL = { return 1; }, +// EGLAPI EGLBoolean EGLAPIENTRY eglTerminate(EGLDisplay dpy); + eglTerminate: function(display) { + if (display != 62000 /* Magic ID for Emscripten 'default display' */) { + EGL.setErrorCode(0x3008 /* EGL_BAD_DISPLAY */); + return 0; + } + // TODO: Tear down EGL here. Currently a no-op since we don't need to actually do anything here for the browser. + EGL.setErrorCode(0x3000 /* EGL_SUCCESS */); + return 1; + }, + // EGLAPI EGLBoolean EGLAPIENTRY eglGetConfigs(EGLDisplay dpy, EGLConfig *configs, EGLint config_size, EGLint *num_config); eglGetConfigs: function(display, configs, config_size, numConfigs) { return EGL.chooseConfig(display, 0, configs, config_size, numConfigs); @@ -225,6 +236,20 @@ var LibraryEGL = { return 62006; /* Magic ID for Emscripten 'default surface' */ }, + // EGLAPI EGLBoolean EGLAPIENTRY eglDestroySurface(EGLDisplay display, EGLSurface surface); + eglDestroySurface: function(display, surface) { + if (display != 62000 /* Magic ID for Emscripten 'default display' */) { + EGL.setErrorCode(0x3008 /* EGL_BAD_DISPLAY */); + return 0; + } + if (surface != 62006 /* Magic ID for the only EGLSurface supported by Emscripten */) { + EGL.setErrorCode(0x300D /* EGL_BAD_SURFACE */); + return 1; + } + EGL.setErrorCode(0x3000 /* EGL_SUCCESS */); + return 1; /* Magic ID for Emscripten 'default surface' */ + }, + eglCreateContext__deps: ['glutCreateWindow', '$GL'], // EGLAPI EGLContext EGLAPIENTRY eglCreateContext(EGLDisplay dpy, EGLConfig config, EGLContext share_context, const EGLint *attrib_list); @@ -234,7 +259,21 @@ var LibraryEGL = { return 0; } - _glutCreateWindow(); + EGL.windowID = _glutCreateWindow(); + EGL.setErrorCode(0x3000 /* EGL_SUCCESS */); + return 62004; // Magic ID for Emscripten EGLContext + }, + + eglDestroyContext__deps: ['glutDestroyWindow', '$GL'], + + // EGLAPI EGLBoolean EGLAPIENTRY eglDestroyContext(EGLDisplay dpy, EGLContext context); + eglDestroyContext: function(display, context) { + if (display != 62000 /* Magic ID for Emscripten 'default display' */) { + EGL.setErrorCode(0x3008 /* EGL_BAD_DISPLAY */); + return 0; + } + + _glutDestroyWindow(EGL.windowID); EGL.setErrorCode(0x3000 /* EGL_SUCCESS */); return 62004; // Magic ID for Emscripten EGLContext }, diff --git a/src/library_gc.js b/src/library_gc.js index 083019ca..f6db74d8 100644 --- a/src/library_gc.js +++ b/src/library_gc.js @@ -4,7 +4,7 @@ if (GC_SUPPORT) { EXPORTED_FUNCTIONS['_realloc'] = 1; var LibraryGC = { - $GC__deps: ['sbrk', 'realloc'], + $GC__deps: ['sbrk', 'realloc', 'calloc'], $GC: { ALLOCATIONS_TO_GC: 1*1024*1024, diff --git a/src/library_gl.js b/src/library_gl.js index f7966dab..c6007809 100644 --- a/src/library_gl.js +++ b/src/library_gl.js @@ -19,6 +19,27 @@ var LibraryGL = { uniforms: [], shaders: [], +#if FULL_ES2 + clientBuffers: [], +#endif + currArrayBuffer: 0, + currElementArrayBuffer: 0, + + byteSizeByTypeRoot: 0x1400, // GL_BYTE + byteSizeByType: [ + 1, // GL_BYTE + 1, // GL_UNSIGNED_BYTE + 2, // GL_SHORT + 2, // GL_UNSIGNED_SHORT + 4, // GL_INT + 4, // GL_UNSIGNED_INT + 4, // GL_FLOAT + 2, // GL_2_BYTES + 3, // GL_3_BYTES + 4, // GL_4_BYTES + 8 // GL_DOUBLE + ], + uniformTable: {}, // name => uniform ID. the uID must be identical until relinking, cannot create a new uID each call to glGetUniformLocation packAlignment: 4, // default alignment is 4 bytes @@ -37,6 +58,65 @@ var LibraryGL = { return ret; }, + // Temporary buffers + MAX_TEMP_BUFFER_SIZE: {{{ GL_MAX_TEMP_BUFFER_SIZE }}}, + tempBufferIndexLookup: null, + tempVertexBuffers: null, + tempIndexBuffers: null, + tempQuadIndexBuffer: null, + + generateTempBuffers: function(quads) { + this.tempBufferIndexLookup = new Uint8Array(this.MAX_TEMP_BUFFER_SIZE+1); + this.tempVertexBuffers = []; + this.tempIndexBuffers = []; + var last = -1, curr = -1; + var size = 1; + for (var i = 0; i <= this.MAX_TEMP_BUFFER_SIZE; i++) { + if (i > size) { + size <<= 1; + } + if (size != last) { + curr++; + this.tempVertexBuffers[curr] = Module.ctx.createBuffer(); + Module.ctx.bindBuffer(Module.ctx.ARRAY_BUFFER, this.tempVertexBuffers[curr]); + Module.ctx.bufferData(Module.ctx.ARRAY_BUFFER, size, Module.ctx.DYNAMIC_DRAW); + Module.ctx.bindBuffer(Module.ctx.ARRAY_BUFFER, null); + this.tempIndexBuffers[curr] = Module.ctx.createBuffer(); + Module.ctx.bindBuffer(Module.ctx.ELEMENT_ARRAY_BUFFER, this.tempIndexBuffers[curr]); + Module.ctx.bufferData(Module.ctx.ELEMENT_ARRAY_BUFFER, size, Module.ctx.DYNAMIC_DRAW); + Module.ctx.bindBuffer(Module.ctx.ELEMENT_ARRAY_BUFFER, null); + last = size; + } + this.tempBufferIndexLookup[i] = curr; + } + + if (quads) { + // GL_QUAD indexes can be precalculated + this.tempQuadIndexBuffer = Module.ctx.createBuffer(); + Module.ctx.bindBuffer(Module.ctx.ELEMENT_ARRAY_BUFFER, this.tempQuadIndexBuffer); + var numIndexes = this.MAX_TEMP_BUFFER_SIZE >> 1; + var quadIndexes = new Uint16Array(numIndexes); + var i = 0, v = 0; + while (1) { + quadIndexes[i++] = v; + if (i >= numIndexes) break; + quadIndexes[i++] = v+1; + if (i >= numIndexes) break; + quadIndexes[i++] = v+2; + if (i >= numIndexes) break; + quadIndexes[i++] = v; + if (i >= numIndexes) break; + quadIndexes[i++] = v+2; + if (i >= numIndexes) break; + quadIndexes[i++] = v+3; + if (i >= numIndexes) break; + v += 4; + } + Module.ctx.bufferData(Module.ctx.ELEMENT_ARRAY_BUFFER, quadIndexes, Module.ctx.STATIC_DRAW); + Module.ctx.bindBuffer(Module.ctx.ELEMENT_ARRAY_BUFFER, null); + } + }, + // Linear lookup in one of the tables (buffers, programs, etc.). TODO: consider using a weakmap to make this faster, if it matters scan: function(table, object) { for (var item in table) { @@ -176,12 +256,70 @@ var LibraryGL = { } }, +#if FULL_ES2 + calcBufLength: function(size, type, stride, count) { + if (stride > 0) { + return count * stride; // XXXvlad this is not exactly correct I don't think + } + var typeSize = GL.byteSizeByType[type - GL.byteSizeByTypeRoot]; + return size * typeSize * count; + }, + + usedTempBuffers: [], + + preDrawHandleClientVertexAttribBindings: function(count) { + GL.resetBufferBinding = false; + + var used = GL.usedTempBuffers; + used.length = 0; + + // TODO: initial pass to detect ranges we need to upload, might not need an upload per attrib + for (var i = 0; i < GL.maxVertexAttribs; ++i) { + var cb = GL.clientBuffers[i]; + if (!cb.enabled) continue; + + GL.resetBufferBinding = true; + + var size = GL.calcBufLength(cb.size, cb.type, cb.stride, count); + var index = GL.tempBufferIndexLookup[size]; + var buf; + do { +#if ASSERTIONS + assert(index < GL.tempVertexBuffers.length); +#endif + buf = GL.tempVertexBuffers[index++]; + } while (used.indexOf(buf) >= 0); + used.push(buf); + Module.ctx.bindBuffer(Module.ctx.ARRAY_BUFFER, buf); + Module.ctx.bufferData(Module.ctx.ARRAY_BUFFER, + HEAPU8.subarray(cb.ptr, cb.ptr + size), + Module.ctx.DYNAMIC_DRAW); + Module.ctx.vertexAttribPointer(i, cb.size, cb.type, cb.normalized, cb.stride, 0); + } + }, + + postDrawHandleClientVertexAttribBindings: function() { + if (GL.resetBufferBinding) { + Module.ctx.bindBuffer(Module.ctx.ARRAY_BUFFER, GL.buffers[GL.currArrayBuffer]); + } + }, +#endif + initExtensions: function() { if (GL.initExtensions.done) return; GL.initExtensions.done = true; if (!Module.useWebGL) return; // an app might link both gl and 2d backends + GL.maxVertexAttribs = Module.ctx.getParameter(Module.ctx.MAX_VERTEX_ATTRIBS); +#if FULL_ES2 + for (var i = 0; i < GL.maxVertexAttribs; i++) { + GL.clientBuffers[i] = { enabled: false, size: 0, type: 0, normalized: 0, stride: 0, ptr: 0 }; + } + + GL.generateTempBuffers(); +#endif + GL.compressionExt = Module.ctx.getExtension('WEBGL_compressed_texture_s3tc') || Module.ctx.getExtension('MOZ_WEBGL_compressed_texture_s3tc') || Module.ctx.getExtension('WEBKIT_WEBGL_compressed_texture_s3tc'); @@ -408,7 +546,19 @@ var LibraryGL = { }, glReadPixels: function(x, y, width, height, format, type, pixels) { - Module.ctx.readPixels(x, y, width, height, format, type, HEAPU8.subarray(pixels)); + assert(type == 0x1401 /* GL_UNSIGNED_BYTE */); + var sizePerPixel; + switch (format) { + case 0x1907 /* GL_RGB */: + sizePerPixel = 3; + break; + case 0x1908 /* GL_RGBA */: + sizePerPixel = 4; + break; + default: throw 'unsupported glReadPixels format'; + } + var totalSize = width*height*sizePerPixel; + Module.ctx.readPixels(x, y, width, height, format, type, HEAPU8.subarray(pixels, pixels + totalSize)); }, glBindTexture: function(target, texture) { @@ -444,6 +594,9 @@ var LibraryGL = { var id = {{{ makeGetValue('buffers', 'i*4', 'i32') }}}; Module.ctx.deleteBuffer(GL.buffers[id]); GL.buffers[id] = null; + + if (id == GL.currArrayBuffer) GL.currArrayBuffer = 0; + if (id == GL.currElementArrayBuffer) GL.currElementArrayBuffer = 0; } }, @@ -462,11 +615,9 @@ var LibraryGL = { }, glIsBuffer: function(buffer) { - var fb = GL.buffers[buffer]; - if (typeof(fb) == 'undefined') { - return 0; - } - return Module.ctx.isBuffer(fb); + var b = GL.buffers[buffer]; + if (!b) return 0; + return Module.ctx.isBuffer(b); }, glGenRenderbuffers__sig: 'vii', @@ -497,11 +648,9 @@ var LibraryGL = { }, glIsRenderbuffer: function(renderbuffer) { - var fb = GL.renderbuffers[renderbuffer]; - if (typeof(fb) == 'undefined') { - return 0; - } - return Module.ctx.isRenderbuffer(fb); + var rb = GL.renderbuffers[renderbuffer]; + if (!rb) return 0; + return Module.ctx.isRenderbuffer(rb); }, glGetUniformfv: function(program, location, params) { @@ -542,6 +691,11 @@ var LibraryGL = { }, glGetVertexAttribfv: function(index, pname, params) { +#if FULL_ES2 + if (GL.clientBuffers[index].enabled) { + Module.printErr("glGetVertexAttribfv on client-side array: not supported, bad data returned"); + } +#endif var data = Module.ctx.getVertexAttrib(index, pname); if (typeof data == 'number') { {{{ makeSetValue('params', '0', 'data', 'float') }}}; @@ -553,6 +707,11 @@ var LibraryGL = { }, glGetVertexAttribiv: function(index, pname, params) { +#if FULL_ES2 + if (GL.clientBuffers[index].enabled) { + Module.printErr("glGetVertexAttribiv on client-side array: not supported, bad data returned"); + } +#endif var data = Module.ctx.getVertexAttrib(index, pname); if (typeof data == 'number' || typeof data == 'boolean') { {{{ makeSetValue('params', '0', 'data', 'i32') }}}; @@ -564,6 +723,11 @@ var LibraryGL = { }, glGetVertexAttribPointerv: function(index, pname, pointer) { +#if FULL_ES2 + if (GL.clientBuffers[index].enabled) { + Module.printErr("glGetVertexAttribPointer on client-side array: not supported, bad data returned"); + } +#endif {{{ makeSetValue('pointer', '0', 'Module.ctx.getVertexAttribOffset(index, pname)', 'i32') }}}; }, @@ -719,6 +883,12 @@ var LibraryGL = { glBindBuffer__sig: 'vii', glBindBuffer: function(target, buffer) { + if (target == Module.ctx.ARRAY_BUFFER) { + GL.currArrayBuffer = buffer; + } else if (target == Module.ctx.ELEMENT_ARRAY_BUFFER) { + GL.currElementArrayBuffer = buffer; + } + Module.ctx.bindBuffer(target, buffer ? GL.buffers[buffer] : null); }, @@ -846,11 +1016,9 @@ var LibraryGL = { }, glIsShader: function(shader) { - var fb = GL.shaders[shader]; - if (typeof(fb) == 'undefined') { - return 0; - } - return Module.ctx.isShader(fb); + var s = GL.shaders[shader]; + if (!s) return 0; + return Module.ctx.isShader(s); }, glCreateProgram__sig: 'i', @@ -908,11 +1076,9 @@ var LibraryGL = { }, glIsProgram: function(program) { - var fb = GL.programs[program]; - if (typeof(fb) == 'undefined') { - return 0; - } - return Module.ctx.isProgram(fb); + var program = GL.programs[program]; + if (!program) return 0; + return Module.ctx.isProgram(program); }, glBindAttribLocation__sig: 'viii', @@ -965,9 +1131,7 @@ var LibraryGL = { glIsFramebuffer__sig: 'ii', glIsFramebuffer: function(framebuffer) { var fb = GL.framebuffers[framebuffer]; - if (typeof(fb) == 'undefined') { - return 0; - } + if (!fb) return 0; return Module.ctx.isFramebuffer(fb); }, @@ -983,6 +1147,11 @@ var LibraryGL = { fogMode: 0x0800, // GL_EXP fogEnabled: false, + // VAO support + vaos: [], + currentVao: null, + enabledVertexAttribArrays: {}, // helps with vao cleanups + init: function() { GLEmulation.fogColor = new Float32Array(4); @@ -1001,6 +1170,7 @@ var LibraryGL = { 0x809E: 1, // GL_SAMPLE_ALPHA_TO_COVERAGE 0x80A0: 1 // GL_SAMPLE_COVERAGE }; + _glEnable = function(cap) { // Clean up the renderer on any change to the rendering state. The optimization of // skipping renderer setup is aimed at the case of multiple glDraw* right after each other @@ -1008,6 +1178,11 @@ var LibraryGL = { if (cap == 0x0B60 /* GL_FOG */) { GLEmulation.fogEnabled = true; return; + } else if (cap == 0x0de1 /* GL_TEXTURE_2D */) { + // XXX not according to spec, and not in desktop GL, but works in some GLES1.x apparently, so support + // it by forwarding to glEnableClientState + _glEnableClientState(cap); + return; } else if (!(cap in validCapabilities)) { return; } @@ -1018,6 +1193,11 @@ var LibraryGL = { if (cap == 0x0B60 /* GL_FOG */) { GLEmulation.fogEnabled = false; return; + } else if (cap == 0x0de1 /* GL_TEXTURE_2D */) { + // XXX not according to spec, and not in desktop GL, but works in some GLES1.x apparently, so support + // it by forwarding to glDisableClientState + _glDisableClientState(cap); + return; } else if (!(cap in validCapabilities)) { return; } @@ -1032,6 +1212,17 @@ var LibraryGL = { return Module.ctx.isEnabled(cap); }; + var glGetBooleanv = _glGetBooleanv; + _glGetBooleanv = function(pname, p) { + var attrib = GLEmulation.getAttributeFromCapability(pname); + if (attrib !== null) { + var result = GL.immediate.enabledClientAttributes[attrib]; + {{{ makeSetValue('p', '0', 'result === true ? 1 : 0', 'i8') }}}; + return; + } + glGetBooleanv(pname, p); + }; + var glGetIntegerv = _glGetIntegerv; _glGetIntegerv = function(pname, params) { switch (pname) { @@ -1052,6 +1243,51 @@ var LibraryGL = { return; } case 0x8871: pname = Module.ctx.MAX_COMBINED_TEXTURE_IMAGE_UNITS /* close enough */; break; // GL_MAX_TEXTURE_COORDS + case 0x807A: { // GL_VERTEX_ARRAY_SIZE + var attribute = GLImmediate.clientAttributes[GLImmediate.VERTEX]; + {{{ makeSetValue('params', '0', 'attribute ? attribute.size : 0', 'i32') }}}; + return; + } + case 0x807B: { // GL_VERTEX_ARRAY_TYPE + var attribute = GLImmediate.clientAttributes[GLImmediate.VERTEX]; + {{{ makeSetValue('params', '0', 'attribute ? attribute.type : 0', 'i32') }}}; + return; + } + case 0x807C: { // GL_VERTEX_ARRAY_STRIDE + var attribute = GLImmediate.clientAttributes[GLImmediate.VERTEX]; + {{{ makeSetValue('params', '0', 'attribute ? attribute.stride : 0', 'i32') }}}; + return; + } + case 0x8081: { // GL_COLOR_ARRAY_SIZE + var attribute = GLImmediate.clientAttributes[GLImmediate.COLOR]; + {{{ makeSetValue('params', '0', 'attribute ? attribute.size : 0', 'i32') }}}; + return; + } + case 0x8082: { // GL_COLOR_ARRAY_TYPE + var attribute = GLImmediate.clientAttributes[GLImmediate.COLOR]; + {{{ makeSetValue('params', '0', 'attribute ? attribute.type : 0', 'i32') }}}; + return; + } + case 0x8083: { // GL_COLOR_ARRAY_STRIDE + var attribute = GLImmediate.clientAttributes[GLImmediate.COLOR]; + {{{ makeSetValue('params', '0', 'attribute ? attribute.stride : 0', 'i32') }}}; + return; + } + case 0x8088: { // GL_TEXTURE_COORD_ARRAY_SIZE + var attribute = GLImmediate.clientAttributes[GLImmediate.TEXTURE0]; + {{{ makeSetValue('params', '0', 'attribute ? attribute.size : 0', 'i32') }}}; + return; + } + case 0x8089: { // GL_TEXTURE_COORD_ARRAY_TYPE + var attribute = GLImmediate.clientAttributes[GLImmediate.TEXTURE0]; + {{{ makeSetValue('params', '0', 'attribute ? attribute.type : 0', 'i32') }}}; + return; + } + case 0x808A: { // GL_TEXTURE_COORD_ARRAY_STRIDE + var attribute = GLImmediate.clientAttributes[GLImmediate.TEXTURE0]; + {{{ makeSetValue('params', '0', 'attribute ? attribute.stride : 0', 'i32') }}}; + return; + } } glGetIntegerv(pname, params); }; @@ -1266,19 +1502,12 @@ var LibraryGL = { _glBindBuffer = function(target, buffer) { glBindBuffer(target, buffer); if (target == Module.ctx.ARRAY_BUFFER) { - GL.currArrayBuffer = buffer; + if (GLEmulation.currentVao) { + assert(GLEmulation.currentVao.arrayBuffer == buffer || GLEmulation.currentVao.arrayBuffer == 0 || buffer == 0, 'TODO: support for multiple array buffers in vao'); + GLEmulation.currentVao.arrayBuffer = buffer; + } } else if (target == Module.ctx.ELEMENT_ARRAY_BUFFER) { - GL.currElementArrayBuffer = buffer; - } - }; - - var glDeleteBuffers = _glDeleteBuffers; - _glDeleteBuffers = function(n, buffers) { - glDeleteBuffers(n, buffers); - for (var i = 0; i < n; i++) { - var buffer = {{{ makeGetValue('buffers', 'i*4', 'i32') }}}; - if (buffer == GL.currArrayBuffer) GL.currArrayBuffer = 0; - if (buffer == GL.currElementArrayBuffer) GL.currElementArrayBuffer = 0; + if (GLEmulation.currentVao) GLEmulation.currentVao.elementArrayBuffer = buffer; } }; @@ -1312,6 +1541,44 @@ var LibraryGL = { } glHint(target, mode); }; + + var glEnableVertexAttribArray = _glEnableVertexAttribArray; + _glEnableVertexAttribArray = function(index) { + glEnableVertexAttribArray(index); + GLEmulation.enabledVertexAttribArrays[index] = 1; + if (GLEmulation.currentVao) GLEmulation.currentVao.enabledVertexAttribArrays[index] = 1; + }; + + var glDisableVertexAttribArray = _glDisableVertexAttribArray; + _glDisableVertexAttribArray = function(index) { + glDisableVertexAttribArray(index); + delete GLEmulation.enabledVertexAttribArrays[index]; + if (GLEmulation.currentVao) delete GLEmulation.currentVao.enabledVertexAttribArrays[index]; + }; + + var glVertexAttribPointer = _glVertexAttribPointer; + _glVertexAttribPointer = function(index, size, type, normalized, stride, pointer) { + glVertexAttribPointer(index, size, type, normalized, stride, pointer); + if (GLEmulation.currentVao) { // TODO: avoid object creation here? likely not hot though + GLEmulation.currentVao.vertexAttribPointers[index] = [index, size, type, normalized, stride, pointer]; + } + }; + }, + + getAttributeFromCapability: function(cap) { + var attrib = null; + switch (cap) { + case 0x8078: // GL_TEXTURE_COORD_ARRAY + case 0x0de1: // GL_TEXTURE_2D - XXX not according to spec, and not in desktop GL, but works in some GLES1.x apparently, so support it + attrib = GL.immediate.TEXTURE0 + GL.immediate.clientActiveTexture; break; + case 0x8074: // GL_VERTEX_ARRAY + attrib = GL.immediate.VERTEX; break; + case 0x8075: // GL_NORMAL_ARRAY + attrib = GL.immediate.NORMAL; break; + case 0x8076: // GL_COLOR_ARRAY + attrib = GL.immediate.COLOR; break; + } + return attrib; }, getProcAddress: function(name) { @@ -1375,6 +1642,9 @@ var LibraryGL = { case 'glIsFramebuffer': ret = {{{ Functions.getIndex('_glIsFramebuffer', true) }}}; break; case 'glCheckFramebufferStatus': ret = {{{ Functions.getIndex('_glCheckFramebufferStatus', true) }}}; break; case 'glRenderbufferStorage': ret = {{{ Functions.getIndex('_glRenderbufferStorage', true) }}}; break; + case 'glGenVertexArrays': ret = {{{ Functions.getIndex('_glGenVertexArrays', true) }}}; break; + case 'glDeleteVertexArrays': ret = {{{ Functions.getIndex('_glDeleteVertexArrays', true) }}}; break; + case 'glBindVertexArray': ret = {{{ Functions.getIndex('_glBindVertexArray', true) }}}; break; } if (!ret) Module.printErr('WARNING: getProcAddress failed for ' + name); return ret; @@ -1427,10 +1697,24 @@ var LibraryGL = { assert(id == 0); }, + glGetPointerv: function(name, p) { + var attribute; + switch(name) { + case 0x808E: // GL_VERTEX_ARRAY_POINTER + attribute = GLImmediate.clientAttributes[GLImmediate.VERTEX]; break; + case 0x8090: // GL_COLOR_ARRAY_POINTER + attribute = GLImmediate.clientAttributes[GLImmediate.COLOR]; break; + case 0x8092: // GL_TEXTURE_COORD_ARRAY_POINTER + attribute = GLImmediate.clientAttributes[GLImmediate.TEXTURE0]; break; + default: throw 'TODO: glGetPointerv for ' + name; + } + {{{ makeSetValue('p', '0', 'attribute ? attribute.pointer : 0', 'i32') }}}; + }, + // GL Immediate mode $GLImmediate__postset: 'GL.immediate.setupFuncs(); Browser.moduleContextCreatedCallbacks.push(function() { GL.immediate.init() });', - $GLImmediate__deps: ['$Browser', '$GL'], + $GLImmediate__deps: ['$Browser', '$GL', '$GLEmulation'], $GLImmediate: { MAX_TEXTURES: 7, @@ -1479,17 +1763,6 @@ var LibraryGL = { clientActiveTexture: 0, clientColor: null, - byteSizeByTypeRoot: 0x1400, // GL_BYTE - byteSizeByType: [ - 1, // GL_BYTE - 1, // GL_UNSIGNED_BYTE - 2, // GL_SHORT - 2, // GL_UNSIGNED_SHORT - 4, // GL_INT - 4, // GL_UNSIGNED_INT - 4 // GL_FLOAT - ], - setClientAttribute: function(name, size, type, stride, pointer) { var attrib = this.clientAttributes[name]; attrib.name = name; @@ -1500,62 +1773,6 @@ var LibraryGL = { this.modifiedClientAttributes = true; }, - // Temporary buffers - MAX_TEMP_BUFFER_SIZE: 2*1024*1024, - tempBufferIndexLookup: null, - tempVertexBuffers: null, - tempIndexBuffers: null, - tempQuadIndexBuffer: null, - - generateTempBuffers: function() { - this.tempBufferIndexLookup = new Uint8Array(this.MAX_TEMP_BUFFER_SIZE+1); - this.tempVertexBuffers = []; - this.tempIndexBuffers = []; - var last = -1, curr = -1; - var size = 1; - for (var i = 0; i <= this.MAX_TEMP_BUFFER_SIZE; i++) { - if (i > size) { - size <<= 1; - } - if (size != last) { - curr++; - this.tempVertexBuffers[curr] = Module.ctx.createBuffer(); - Module.ctx.bindBuffer(Module.ctx.ARRAY_BUFFER, this.tempVertexBuffers[curr]); - Module.ctx.bufferData(Module.ctx.ARRAY_BUFFER, size, Module.ctx.DYNAMIC_DRAW); - Module.ctx.bindBuffer(Module.ctx.ARRAY_BUFFER, null); - this.tempIndexBuffers[curr] = Module.ctx.createBuffer(); - Module.ctx.bindBuffer(Module.ctx.ELEMENT_ARRAY_BUFFER, this.tempIndexBuffers[curr]); - Module.ctx.bufferData(Module.ctx.ELEMENT_ARRAY_BUFFER, size, Module.ctx.DYNAMIC_DRAW); - Module.ctx.bindBuffer(Module.ctx.ELEMENT_ARRAY_BUFFER, null); - last = size; - } - this.tempBufferIndexLookup[i] = curr; - } - // GL_QUAD indexes can be precalculated - this.tempQuadIndexBuffer = Module.ctx.createBuffer(); - Module.ctx.bindBuffer(Module.ctx.ELEMENT_ARRAY_BUFFER, this.tempQuadIndexBuffer); - var numIndexes = this.MAX_TEMP_BUFFER_SIZE >> 1; - var quadIndexes = new Uint16Array(numIndexes); - var i = 0, v = 0; - while (1) { - quadIndexes[i++] = v; - if (i >= numIndexes) break; - quadIndexes[i++] = v+1; - if (i >= numIndexes) break; - quadIndexes[i++] = v+2; - if (i >= numIndexes) break; - quadIndexes[i++] = v; - if (i >= numIndexes) break; - quadIndexes[i++] = v+2; - if (i >= numIndexes) break; - quadIndexes[i++] = v+3; - if (i >= numIndexes) break; - v += 4; - } - Module.ctx.bufferData(Module.ctx.ELEMENT_ARRAY_BUFFER, quadIndexes, Module.ctx.STATIC_DRAW); - Module.ctx.bindBuffer(Module.ctx.ELEMENT_ARRAY_BUFFER, null); - }, - // Renderers addRendererComponent: function(name, size, type) { if (!this.rendererComponents[name]) { @@ -1565,7 +1782,7 @@ var LibraryGL = { #endif this.enabledClientAttributes[name] = true; this.setClientAttribute(name, size, type, 0, this.rendererComponentPointer); - this.rendererComponentPointer += size * this.byteSizeByType[type - this.byteSizeByTypeRoot]; + this.rendererComponentPointer += size * GL.byteSizeByType[type - GL.byteSizeByTypeRoot]; } else { this.rendererComponents[name]++; } @@ -1589,7 +1806,7 @@ var LibraryGL = { cacheItem = temp ? temp : (cacheItem[attribute.name] = GL.immediate.rendererCacheItemTemplate.slice()); temp = cacheItem[attribute.size]; cacheItem = temp ? temp : (cacheItem[attribute.size] = GL.immediate.rendererCacheItemTemplate.slice()); - var typeIndex = attribute.type - GL.immediate.byteSizeByTypeRoot; // ensure it starts at 0 to keep the cache items dense + var typeIndex = attribute.type - GL.byteSizeByTypeRoot; // ensure it starts at 0 to keep the cache items dense temp = cacheItem[typeIndex]; cacheItem = temp ? temp : (cacheItem[typeIndex] = GL.immediate.rendererCacheItemTemplate.slice()); } @@ -1776,35 +1993,43 @@ var LibraryGL = { if (!GL.currArrayBuffer) { var start = GL.immediate.firstVertex*GL.immediate.stride; var end = GL.immediate.lastVertex*GL.immediate.stride; - assert(end <= GL.immediate.MAX_TEMP_BUFFER_SIZE, 'too much vertex data'); - arrayBuffer = GL.immediate.tempVertexBuffers[GL.immediate.tempBufferIndexLookup[end]]; + assert(end <= GL.MAX_TEMP_BUFFER_SIZE, 'too much vertex data'); + arrayBuffer = GL.tempVertexBuffers[GL.tempBufferIndexLookup[end]]; // TODO: consider using the last buffer we bound, if it was larger. downside is larger buffer, but we might avoid rebinding and preparing } else { arrayBuffer = GL.currArrayBuffer; } // If the array buffer is unchanged and the renderer as well, then we can avoid all the work here - // XXX We use some heuristics here, and this may not work in all cases. Try disabling this if you - // have odd glitches (by setting canSkip always to 0, or even cleaning up the renderer right - // after rendering) + // XXX We use some heuristics here, and this may not work in all cases. Try disabling GL_UNSAFE_OPTS if you + // have odd glitches +#if GL_UNSAFE_OPTS var lastRenderer = GL.immediate.lastRenderer; var canSkip = this == lastRenderer && arrayBuffer == GL.immediate.lastArrayBuffer && (GL.currProgram || this.program) == GL.immediate.lastProgram && !GL.immediate.matricesModified; if (!canSkip && lastRenderer) lastRenderer.cleanup(); +#endif if (!GL.currArrayBuffer) { // Bind the array buffer and upload data after cleaning up the previous renderer +#if GL_UNSAFE_OPTS + // Potentially unsafe, since lastArrayBuffer might not reflect the true array buffer in code that mixes immediate/non-immediate if (arrayBuffer != GL.immediate.lastArrayBuffer) { +#endif Module.ctx.bindBuffer(Module.ctx.ARRAY_BUFFER, arrayBuffer); +#if GL_UNSAFE_OPTS } +#endif Module.ctx.bufferSubData(Module.ctx.ARRAY_BUFFER, start, GL.immediate.vertexData.subarray(start >> 2, end >> 2)); } +#if GL_UNSAFE_OPTS if (canSkip) return; GL.immediate.lastRenderer = this; GL.immediate.lastArrayBuffer = arrayBuffer; GL.immediate.lastProgram = GL.currProgram || this.program; GL.immediate.matricesModified = false; +#endif if (!GL.currProgram) { Module.ctx.useProgram(this.program); @@ -1880,9 +2105,11 @@ var LibraryGL = { Module.ctx.bindBuffer(Module.ctx.ARRAY_BUFFER, null); } +#if GL_UNSAFE_OPTS GL.immediate.lastRenderer = null; GL.immediate.lastArrayBuffer = null; GL.immediate.lastProgram = null; +#endif GL.immediate.matricesModified = true; } }; @@ -1958,12 +2185,12 @@ var LibraryGL = { this.rendererCache = this.rendererCacheItemTemplate.slice(); // Buffers for data - this.tempData = new Float32Array(this.MAX_TEMP_BUFFER_SIZE >> 2); - this.indexData = new Uint16Array(this.MAX_TEMP_BUFFER_SIZE >> 1); + this.tempData = new Float32Array(GL.MAX_TEMP_BUFFER_SIZE >> 2); + this.indexData = new Uint16Array(GL.MAX_TEMP_BUFFER_SIZE >> 1); this.vertexDataU8 = new Uint8Array(this.tempData.buffer); - this.generateTempBuffers(); + GL.generateTempBuffers(true); this.clientColor = new Float32Array([1, 1, 1, 1]); }, @@ -1997,22 +2224,61 @@ var LibraryGL = { #endif if (attribute.stride) stride = attribute.stride; } - - var bytes = 0; - for (var i = 0; i < attributes.length; i++) { - var attribute = attributes[i]; - if (!attribute) break; - attribute.offset = attribute.pointer - start; - if (attribute.offset > bytes) { // ensure we start where we should - assert((attribute.offset - bytes)%4 == 0); // XXX assuming 4-alignment - bytes += attribute.offset - bytes; + var bytes = 0; // total size in bytes + if (!stride && !beginEnd) { + // beginEnd can not have stride in the attributes, that is fine. otherwise, + // no stride means that all attributes are in fact packed. to keep the rest of + // our emulation code simple, we perform unpacking/restriding here. this adds overhead, so + // it is a good idea to not hit this! +#if ASSERTIONS + Runtime.warnOnce('Unpacking/restriding attributes, this is not fast'); +#endif + if (!GL.immediate.restrideBuffer) GL.immediate.restrideBuffer = _malloc(GL.MAX_TEMP_BUFFER_SIZE); + start = GL.immediate.restrideBuffer; +#if ASSERTIONS + assert(start % 4 == 0); +#endif + // calculate restrided offsets and total size + for (var i = 0; i < attributes.length; i++) { + var attribute = attributes[i]; + if (!attribute) break; + var size = attribute.size * GL.byteSizeByType[attribute.type - GL.byteSizeByTypeRoot]; + if (size % 4 != 0) size += 4 - (size % 4); // align everything + attribute.offset = bytes; + bytes += size; + } +#if ASSERTIONS + assert(count*bytes <= GL.MAX_TEMP_BUFFER_SIZE); +#endif + // copy out the data (we need to know the stride for that, and define attribute.pointer + for (var i = 0; i < attributes.length; i++) { + var attribute = attributes[i]; + if (!attribute) break; + var size4 = Math.floor((attribute.size * GL.byteSizeByType[attribute.type - GL.byteSizeByTypeRoot])/4); + for (var j = 0; j < count; j++) { + for (var k = 0; k < size4; k++) { // copy in chunks of 4 bytes, our alignment makes this possible + HEAP32[((start + attribute.offset + bytes*j)>>2) + k] = HEAP32[(attribute.pointer>>2) + j*size4 + k]; + } + } + attribute.pointer = start + attribute.offset; + } + } else { + // normal situation, everything is strided and in the same buffer + for (var i = 0; i < attributes.length; i++) { + var attribute = attributes[i]; + if (!attribute) break; + attribute.offset = attribute.pointer - start; + if (attribute.offset > bytes) { // ensure we start where we should + assert((attribute.offset - bytes)%4 == 0); // XXX assuming 4-alignment + bytes += attribute.offset - bytes; + } + bytes += attribute.size * GL.byteSizeByType[attribute.type - GL.byteSizeByTypeRoot]; + if (bytes % 4 != 0) bytes += 4 - (bytes % 4); // XXX assuming 4-alignment + } + assert(beginEnd || bytes <= stride); // if not begin-end, explicit stride should make sense with total byte size + if (bytes < stride) { // ensure the size is that of the stride + bytes = stride; } - bytes += attribute.size * GL.immediate.byteSizeByType[attribute.type - GL.immediate.byteSizeByTypeRoot]; - if (bytes % 4 != 0) bytes += 4 - (bytes % 4); // XXX assuming 4-alignment - } - assert(stride == 0 || bytes <= stride); - if (bytes < stride) { // ensure the size is that of the stride - bytes = stride; } GL.immediate.stride = bytes; @@ -2026,6 +2292,9 @@ var LibraryGL = { }, flush: function(numProvidedIndexes, startIndex, ptr) { +#if ASSERTIONS + assert(numProvidedIndexes >= 0 || !numProvidedIndexes); +#endif startIndex = startIndex || 0; ptr = ptr || 0; @@ -2053,8 +2322,8 @@ var LibraryGL = { } if (!GL.currElementArrayBuffer) { // If no element array buffer is bound, then indices is a literal pointer to clientside data - assert(numProvidedIndexes << 1 <= GL.immediate.MAX_TEMP_BUFFER_SIZE, 'too many immediate mode indexes (a)'); - var indexBuffer = GL.immediate.tempIndexBuffers[GL.immediate.tempBufferIndexLookup[numProvidedIndexes << 1]]; + assert(numProvidedIndexes << 1 <= GL.MAX_TEMP_BUFFER_SIZE, 'too many immediate mode indexes (a)'); + var indexBuffer = GL.tempIndexBuffers[GL.tempBufferIndexLookup[numProvidedIndexes << 1]]; Module.ctx.bindBuffer(Module.ctx.ELEMENT_ARRAY_BUFFER, indexBuffer); Module.ctx.bufferSubData(Module.ctx.ELEMENT_ARRAY_BUFFER, 0, {{{ makeHEAPView('U16', 'ptr', 'ptr + (numProvidedIndexes << 1)') }}}); ptr = 0; @@ -2069,8 +2338,8 @@ var LibraryGL = { ptr = GL.immediate.firstVertex*3; var numQuads = numVertexes / 4; numIndexes = numQuads * 6; // 0 1 2, 0 2 3 pattern - assert(ptr + (numIndexes << 1) <= GL.immediate.MAX_TEMP_BUFFER_SIZE, 'too many immediate mode indexes (b)'); - Module.ctx.bindBuffer(Module.ctx.ELEMENT_ARRAY_BUFFER, this.tempQuadIndexBuffer); + assert(ptr + (numIndexes << 1) <= GL.MAX_TEMP_BUFFER_SIZE, 'too many immediate mode indexes (b)'); + Module.ctx.bindBuffer(Module.ctx.ELEMENT_ARRAY_BUFFER, GL.tempQuadIndexBuffer); emulatedElementArrayBuffer = true; } @@ -2085,6 +2354,10 @@ var LibraryGL = { if (emulatedElementArrayBuffer) { Module.ctx.bindBuffer(Module.ctx.ELEMENT_ARRAY_BUFFER, GL.buffers[GL.currElementArrayBuffer] || null); } + +#if GL_UNSAFE_OPTS == 0 + renderer.cleanup(); +#endif } }, @@ -2120,7 +2393,7 @@ var LibraryGL = { GL.immediate.vertexData[GL.immediate.vertexCounter++] = y; GL.immediate.vertexData[GL.immediate.vertexCounter++] = z || 0; #if ASSERTIONS - assert(GL.immediate.vertexCounter << 2 < GL.immediate.MAX_TEMP_BUFFER_SIZE); + assert(GL.immediate.vertexCounter << 2 < GL.MAX_TEMP_BUFFER_SIZE); #endif GL.immediate.addRendererComponent(GL.immediate.VERTEX, 3, Module.ctx.FLOAT); }, @@ -2134,8 +2407,10 @@ var LibraryGL = { glVertex2fv: function(p) { _glVertex3f({{{ makeGetValue('p', '0', 'float') }}}, {{{ makeGetValue('p', '4', 'float') }}}, 0); }, + + glVertex3i: 'glVertex3f', - glVertex2i: function() { throw 'glVertex2i: TODO' }, + glVertex2i: 'glVertex3f', glTexCoord2i: function(u, v) { #if ASSERTIONS @@ -2299,25 +2574,21 @@ var LibraryGL = { // ClientState/gl*Pointer glEnableClientState: function(cap, disable) { - var attrib; - switch(cap) { - case 0x8078: // GL_TEXTURE_COORD_ARRAY - attrib = GL.immediate.TEXTURE0 + GL.immediate.clientActiveTexture; break; - case 0x8074: // GL_VERTEX_ARRAY - attrib = GL.immediate.VERTEX; break; - case 0x8075: // GL_NORMAL_ARRAY - attrib = GL.immediate.NORMAL; break; - case 0x8076: // GL_COLOR_ARRAY - attrib = GL.immediate.COLOR; break; - default: - throw 'unhandled clientstate: ' + cap; + var attrib = GLEmulation.getAttributeFromCapability(cap); + if (attrib === null) { +#if ASSERTIONS + Module.printErr('WARNING: unhandled clientstate: ' + cap); +#endif + return; } if (disable && GL.immediate.enabledClientAttributes[attrib]) { GL.immediate.enabledClientAttributes[attrib] = false; GL.immediate.totalEnabledClientAttributes--; + if (GLEmulation.currentVao) delete GLEmulation.currentVao.enabledClientStates[cap]; } else if (!disable && !GL.immediate.enabledClientAttributes[attrib]) { GL.immediate.enabledClientAttributes[attrib] = true; GL.immediate.totalEnabledClientAttributes++; + if (GLEmulation.currentVao) GLEmulation.currentVao.enabledClientStates[cap] = 1; } GL.immediate.modifiedClientAttributes = true; }, @@ -2344,6 +2615,63 @@ var LibraryGL = { GL.immediate.clientActiveTexture = texture - 0x84C0; // GL_TEXTURE0 }, + // Vertex array object (VAO) support. TODO: when the WebGL extension is popular, use that and remove this code and GL.vaos + glGenVertexArrays__deps: ['$GLEMulation'], + glGenVertexArrays__sig: ['vii'], + glGenVertexArrays: function(n, vaos) { + for (var i = 0; i < n; i++) { + var id = GL.getNewId(GLEmulation.vaos); + GLEmulation.vaos[id] = { + id: id, + arrayBuffer: 0, + elementArrayBuffer: 0, + enabledVertexAttribArrays: {}, + vertexAttribPointers: {}, + enabledClientStates: {}, + }; + {{{ makeSetValue('vaos', 'i*4', 'id', 'i32') }}}; + } + }, + glDeleteVertexArrays__sig: ['vii'], + glDeleteVertexArrays: function(n, vaos) { + for (var i = 0; i < n; i++) { + var id = {{{ makeGetValue('vaos', 'i*4', 'i32') }}}; + GLEmulation.vaos[id] = null; + if (GLEmulation.currentVao && GLEmulation.currentVao.id == id) GLEmulation.currentVao = null; + } + }, + glBindVertexArray__sig: ['vi'], + glBindVertexArray: function(vao) { + // undo vao-related things, wipe the slate clean, both for vao of 0 or an actual vao + GLEmulation.currentVao = null; // make sure the commands we run here are not recorded + if (GL.immediate.lastRenderer) GL.immediate.lastRenderer.cleanup(); + _glBindBuffer(Module.ctx.ARRAY_BUFFER, 0); // XXX if one was there before we were bound? + _glBindBuffer(Module.ctx.ELEMENT_ARRAY_BUFFER, 0); + for (var vaa in GLEmulation.enabledVertexAttribArrays) { + Module.ctx.disableVertexAttribArray(vaa); + } + GLEmulation.enabledVertexAttribArrays = {}; + GL.immediate.enabledClientAttributes = [0, 0]; + GL.immediate.totalEnabledClientAttributes = 0; + GL.immediate.modifiedClientAttributes = true; + if (vao) { + // replay vao + var info = GLEmulation.vaos[vao]; + _glBindBuffer(Module.ctx.ARRAY_BUFFER, info.arrayBuffer); // XXX overwrite current binding? + _glBindBuffer(Module.ctx.ELEMENT_ARRAY_BUFFER, info.elementArrayBuffer); + for (var vaa in info.enabledVertexAttribArrays) { + _glEnableVertexAttribArray(vaa); + } + for (var vaa in info.vertexAttribPointers) { + _glVertexAttribPointer.apply(null, info.vertexAttribPointers[vaa]); + } + for (var attrib in info.enabledClientStates) { + _glEnableClientState(attrib|0); + } + GLEmulation.currentVao = info; // set currentVao last, so the commands we ran here were not recorded + } + }, + // OpenGL Immediate Mode matrix routines. // Note that in the future we might make these available only in certain modes. glMatrixMode__deps: ['$GL', '$GLImmediateSetup', '$GLEmulation'], // emulation is not strictly needed, this is a workaround @@ -2434,6 +2762,7 @@ var LibraryGL = { GL.immediate.matrix.lib.mat4.multiply(GL.immediate.matrix[GL.immediate.currentMatrix], GL.immediate.matrix.lib.mat4.frustum(left, right, bottom, top_, nearVal, farVal)); }, + glFrustumf: 'glFrustum', glOrtho: function(left, right, bottom, top_, nearVal, farVal) { GL.immediate.matricesModified = true; @@ -2464,8 +2793,9 @@ var LibraryGL = { gluPerspective: function(fov, aspect, near, far) { GL.immediate.matricesModified = true; - GL.immediate.matrix.lib.mat4.multiply(GL.immediate.matrix[GL.immediate.currentMatrix], - GL.immediate.matrix.lib.mat4.perspective(fov, aspect, near, far, GL.immediate.currentMatrix)); + GL.immediate.matrix[GL.immediate.currentMatrix] = + GL.immediate.matrix.lib.mat4.perspective(fov, aspect, near, far, + GL.immediate.matrix[GL.immediate.currentMatrix]); }, gluLookAt: function(ex, ey, ez, cx, cy, cz, ux, uy, uz) { @@ -2534,36 +2864,140 @@ var LibraryGL = { glTexGeni: function() { throw 'glTexGeni: TODO' }, glTexGenfv: function() { throw 'glTexGenfv: TODO' }, - glTexEnvi: function() { throw 'glTexEnvi: TODO' }, - glTexEnvfv: function() { throw 'glTexEnvfv: TODO' }, + glTexEnvi: function() { Runtime.warnOnce('glTexEnvi: TODO') }, + glTexEnvfv: function() { Runtime.warnOnce('glTexEnvfv: TODO') }, glTexImage1D: function() { throw 'glTexImage1D: TODO' }, glTexCoord3f: function() { throw 'glTexCoord3f: TODO' }, glGetTexLevelParameteriv: function() { throw 'glGetTexLevelParameteriv: TODO' }, - // signatures of simple pass-through functions, see later - glActiveTexture__sig: 'vi', + glShadeModel: function() { Runtime.warnOnce('TODO: glShadeModel') }, + + // GLES2 emulation + + glVertexAttribPointer__sig: 'viiiiii', + glVertexAttribPointer: function(index, size, type, normalized, stride, ptr) { +#if FULL_ES2 + var cb = GL.clientBuffers[index]; +#if ASSERTIONS + assert(cb, index); +#endif + if (!GL.currArrayBuffer) { + cb.size = size; + cb.type = type; + cb.normalized = normalized; + cb.stride = stride; + cb.ptr = ptr; + return; + } + cb.enabled = false; +#endif + Module.ctx.vertexAttribPointer(index, size, type, normalized, stride, ptr); + }, + glEnableVertexAttribArray__sig: 'vi', + glEnableVertexAttribArray: function(index) { +#if FULL_ES2 + var cb = GL.clientBuffers[index]; +#if ASSERTIONS + assert(cb, index); +#endif + cb.enabled = true; +#endif + Module.ctx.enableVertexAttribArray(index); + }, + glDisableVertexAttribArray__sig: 'vi', - glVertexAttribPointer__sig: 'viiiiii', + glDisableVertexAttribArray: function(index) { +#if FULL_ES2 + var cb = GL.clientBuffers[index]; +#if ASSERTIONS + assert(cb, index); +#endif + cb.enabled = false; +#endif + Module.ctx.disableVertexAttribArray(index); + }, + + glDrawArrays: function(mode, first, count) { +#if FULL_ES2 + // bind any client-side buffers + GL.preDrawHandleClientVertexAttribBindings(first + count); +#endif + + Module.ctx.drawArrays(mode, first, count); + +#if FULL_ES2 + GL.postDrawHandleClientVertexAttribBindings(); +#endif + }, + + glDrawElements: function(mode, count, type, indices) { +#if FULL_ES2 + var buf; + if (!GL.currElementArrayBuffer) { + var size = GL.calcBufLength(1, type, 0, count); + buf = GL.tempIndexBuffers[GL.tempBufferIndexLookup[size]]; + Module.ctx.bindBuffer(Module.ctx.ELEMENT_ARRAY_BUFFER, buf); + Module.ctx.bufferData(Module.ctx.ELEMENT_ARRAY_BUFFER, + HEAPU8.subarray(indices, indices + size), + Module.ctx.DYNAMIC_DRAW); + // the index is now 0 + indices = 0; + } + + // bind any client-side buffers + GL.preDrawHandleClientVertexAttribBindings(count); +#endif + + Module.ctx.drawElements(mode, count, type, indices); + +#if FULL_ES2 + GL.postDrawHandleClientVertexAttribBindings(count); + + if (!GL.currElementArrayBuffer) { + Module.ctx.bindBuffer(Module.ctx.ELEMENT_ARRAY_BUFFER, null); + } +#endif + }, + + // signatures of simple pass-through functions, see later + + glActiveTexture__sig: 'vi', glCheckFramebufferStatus__sig: 'ii', glRenderbufferStorage__sig: 'viiii', + + // Open GLES1.1 compatibility + + glGenFramebuffersOES : 'glGenFramebuffers', + glGenRenderbuffersOES : 'glGenRenderbuffers', + glBindFramebufferOES : 'glBindFramebuffer', + glBindRenderbufferOES : 'glBindRenderbuffer', + glGetRenderbufferParameterivOES : 'glGetRenderbufferParameteriv', + glFramebufferRenderbufferOES : 'glFramebufferRenderbuffer', + glRenderbufferStorageOES : 'glRenderbufferStorage', + glCheckFramebufferStatusOES : 'glCheckFramebufferStatus', + glDeleteFramebuffersOES : 'glDeleteFramebuffers', + glDeleteRenderbuffersOES : 'glDeleteRenderbuffers', + glGenVertexArraysOES: 'glGenVertexArrays', + glDeleteVertexArraysOES: 'glDeleteVertexArrays', + glBindVertexArrayOES: 'glBindVertexArray', + glFramebufferTexture2DOES: 'glFramebufferTexture2D' }; // Simple pass-through functions. Starred ones have return values. [X] ones have X in the C name but not in the JS name -[[0, 'shadeModel getError* finish flush'], - [1, 'clearDepth clearDepth[f] depthFunc enable disable frontFace cullFace clear enableVertexAttribArray disableVertexAttribArray lineWidth clearStencil depthMask stencilMask checkFramebufferStatus* generateMipmap activeTexture blendEquation sampleCoverage isEnabled*'], +[[0, 'getError* finish flush'], + [1, 'clearDepth clearDepth[f] depthFunc enable disable frontFace cullFace clear lineWidth clearStencil depthMask stencilMask checkFramebufferStatus* generateMipmap activeTexture blendEquation sampleCoverage isEnabled*'], [2, 'blendFunc blendEquationSeparate depthRange depthRange[f] stencilMaskSeparate hint polygonOffset'], - [3, 'texParameteri texParameterf drawArrays vertexAttrib2f stencilFunc stencilOp'], - [4, 'viewport clearColor scissor vertexAttrib3f colorMask drawElements renderbufferStorage blendFuncSeparate blendColor stencilFuncSeparate stencilOpSeparate'], + [3, 'texParameteri texParameterf vertexAttrib2f stencilFunc stencilOp'], + [4, 'viewport clearColor scissor vertexAttrib3f colorMask renderbufferStorage blendFuncSeparate blendColor stencilFuncSeparate stencilOpSeparate'], [5, 'vertexAttrib4f'], - [6, 'vertexAttribPointer'], [8, 'copyTexImage2D copyTexSubImage2D']].forEach(function(data) { var num = data[0]; var names = data[1]; var args = range(num).map(function(i) { return 'x' + i }).join(', '); - var plainStub = '(function(' + args + ') { ' + (num > 0 ? 'Module.ctx.NAME(' + args + ')' : '') + ' })'; - var returnStub = '(function(' + args + ') { ' + (num > 0 ? 'return Module.ctx.NAME(' + args + ')' : '') + ' })'; + var plainStub = '(function(' + args + ') { Module.ctx.NAME(' + args + ') })'; + var returnStub = '(function(' + args + ') { return Module.ctx.NAME(' + args + ') })'; names.split(' ').forEach(function(name) { var stub = plainStub; if (name[name.length-1] == '*') { @@ -2596,5 +3030,10 @@ LibraryGL.$GLEmulation__deps.push(function() { for (var func in Functions.getIndex.tentative) Functions.getIndex(func); }); +if (FORCE_GL_EMULATION) { + LibraryGL.glDrawElements__deps = LibraryGL.glDrawElements__deps.concat('$GLEmulation'); + LibraryGL.glDrawArrays__deps = LibraryGL.glDrawArrays__deps.concat('$GLEmulation'); +} + mergeInto(LibraryManager.library, LibraryGL); diff --git a/src/library_glut.js b/src/library_glut.js index 2e662698..bb4dfefa 100644 --- a/src/library_glut.js +++ b/src/library_glut.js @@ -381,6 +381,12 @@ var LibraryGLUT = { return 1; }, + glutDestroyWindow__deps: ['$Browser'], + glutDestroyWindow: function(name) { + Module.ctx = Browser.destroyContext(Module['canvas'], true, true); + return 1; + }, + glutReshapeWindow__deps: ['$GLUT', 'glutPostRedisplay'], glutReshapeWindow: function(width, height) { GLUT.cancelFullScreen(); diff --git a/src/library_sdl.js b/src/library_sdl.js index cfab6410..5033b27e 100644 --- a/src/library_sdl.js +++ b/src/library_sdl.js @@ -44,6 +44,8 @@ var LibrarySDL = { ctrlKey: false, altKey: false, + textInput: false, + startTime: null, mouseX: 0, mouseY: 0, @@ -173,6 +175,11 @@ var LibrarySDL = { ['i16', 'mod'], ['i32', 'unicode'] ]), + TextInputEvent: Runtime.generateStructInfo([ + ['i32', 'type'], + ['i32', 'windowID'], + ['b256', 'text'], + ]), MouseMotionEvent: Runtime.generateStructInfo([ ['i32', 'type'], ['i32', 'windowID'], @@ -373,7 +380,7 @@ var LibrarySDL = { } } // fall through - case 'keydown': case 'keyup': case 'mousedown': case 'mouseup': case 'DOMMouseScroll': case 'mousewheel': + case 'keydown': case 'keyup': case 'keypress': case 'mousedown': case 'mouseup': case 'DOMMouseScroll': case 'mousewheel': if (event.type == 'DOMMouseScroll' || event.type == 'mousewheel') { var button = (event.type == 'DOMMouseScroll' ? event.detail : -event.wheelDelta) > 0 ? 4 : 3; var event2 = { @@ -397,6 +404,10 @@ var LibrarySDL = { SDL.DOMButtons[event.button] = 0; } + if (event.type == 'keypress' && !SDL.textInput) { + break; + } + SDL.events.push(event); if (SDL.events.length >= 10000) { Module.printErr('SDL event queue full, dropping earliest event'); @@ -476,6 +487,15 @@ var LibrarySDL = { break; } + case 'keypress': { + {{{ makeSetValue('ptr', 'SDL.structs.TextInputEvent.type', 'SDL.DOMEventToSDLEvent[event.type]', 'i32') }}} + // Not filling in windowID for now + var cStr = intArrayFromString(String.fromCharCode(event.charCode)); + for (var i = 0; i < cStr.length; ++i) { + {{{ makeSetValue('ptr', 'SDL.structs.TextInputEvent.text + i', 'cStr[i]', 'i8') }}}; + } + break; + } case 'mousedown': case 'mouseup': if (event.type == 'mousedown') { // SDL_BUTTON(x) is defined as (1 << ((x)-1)). SDL buttons are 1-3, @@ -607,15 +627,18 @@ var LibrarySDL = { SDL_Init: function(what) { SDL.startTime = Date.now(); // capture all key events. we just keep down and up, but also capture press to prevent default actions - document.onkeydown = SDL.receiveEvent; - document.onkeyup = SDL.receiveEvent; - document.onkeypress = SDL.receiveEvent; + if (!Module['doNotCaptureKeyboard']) { + document.onkeydown = SDL.receiveEvent; + document.onkeyup = SDL.receiveEvent; + document.onkeypress = SDL.receiveEvent; + } window.onunload = SDL.receiveEvent; SDL.keyboardState = _malloc(0x10000); _memset(SDL.keyboardState, 0, 0x10000); // Initialize this structure carefully for closure SDL.DOMEventToSDLEvent['keydown'] = 0x300 /* SDL_KEYDOWN */; SDL.DOMEventToSDLEvent['keyup'] = 0x301 /* SDL_KEYUP */; + SDL.DOMEventToSDLEvent['keypress'] = 0x303 /* SDL_TEXTINPUT */; SDL.DOMEventToSDLEvent['mousedown'] = 0x401 /* SDL_MOUSEBUTTONDOWN */; SDL.DOMEventToSDLEvent['mouseup'] = 0x402 /* SDL_MOUSEBUTTONUP */; SDL.DOMEventToSDLEvent['mousemove'] = 0x400 /* SDL_MOUSEMOTION */; @@ -638,8 +661,8 @@ var LibrarySDL = { {{{ makeSetValue('ret+Runtime.QUANTUM_SIZE*0', '0', '0', 'i32') }}} // TODO {{{ makeSetValue('ret+Runtime.QUANTUM_SIZE*1', '0', '0', 'i32') }}} // TODO {{{ makeSetValue('ret+Runtime.QUANTUM_SIZE*2', '0', '0', 'void*') }}} - {{{ makeSetValue('ret+Runtime.QUANTUM_SIZE*3', '0', 'SDL.defaults.width', 'i32') }}} - {{{ makeSetValue('ret+Runtime.QUANTUM_SIZE*4', '0', 'SDL.defaults.height', 'i32') }}} + {{{ makeSetValue('ret+Runtime.QUANTUM_SIZE*3', '0', 'Module["canvas"].width', 'i32') }}} + {{{ makeSetValue('ret+Runtime.QUANTUM_SIZE*4', '0', 'Module["canvas"].height', 'i32') }}} return ret; }, @@ -896,7 +919,26 @@ var LibrarySDL = { }, SDL_ShowCursor: function(toggle) { - // TODO + switch (toggle) { + case 0: // SDL_DISABLE + if (Browser.isFullScreen) { // only try to lock the pointer when in full screen mode + Module['canvas'].requestPointerLock(); + return 0; + } else { // else return SDL_ENABLE to indicate the failure + return 1; + } + break; + case 1: // SDL_ENABLE + Module['canvas'].exitPointerLock(); + return 1; + break; + case -1: // SDL_QUERY + return !Browser.pointerLock; + break; + default: + console.log( "SDL_ShowCursor called with unknown toggle parameter value: " + toggle + "." ); + break; + } }, SDL_GetError: function() { @@ -1052,6 +1094,15 @@ var LibrarySDL = { }, SDL_WM_GrabInput: function() {}, + + SDL_WM_ToggleFullScreen: function(surf) { + if (Browser.isFullScreen) { + Module['canvas'].cancelFullScreen(); + return 1; + } else { + return 0; + } + }, // SDL_Image @@ -1077,7 +1128,9 @@ var LibrarySDL = { } var surf = SDL.makeSurface(raw.width, raw.height, 0, false, 'load:' + filename); var surfData = SDL.surfaces[surf]; + surfData.ctx.globalCompositeOperation = "copy"; surfData.ctx.drawImage(raw, 0, 0, raw.width, raw.height, 0, 0, raw.width, raw.height); + surfData.ctx.globalCompositeOperation = "source-over"; // XXX SDL does not specify that loaded images must have available pixel data, in fact // there are cases where you just want to blit them, so you just need the hardware // accelerated version. However, code everywhere seems to assume that the pixels @@ -1171,11 +1224,21 @@ var LibrarySDL = { SDL_CondWait: function() {}, SDL_DestroyCond: function() {}, - SDL_StartTextInput: function() {}, // TODO - SDL_StopTextInput: function() {}, // TODO + SDL_StartTextInput: function() { + SDL.textInput = true; + }, + SDL_StopTextInput: function() { + SDL.textInput = false; + }, // SDL Mixer + Mix_Init: function(flags) { + if (!flags) return 0; + return 8; /* MIX_INIT_OGG */ + }, + Mix_Quit: function(){}, + Mix_OpenAudio: function(frequency, format, channels, chunksize) { SDL.allocateChannels(32); // Just record the values for a later call to Mix_QuickLoad_RAW @@ -1280,6 +1343,8 @@ var LibrarySDL = { // the browser has already preloaded the audio file. var channelInfo = SDL.channels[channel]; channelInfo.audio = audio = audio.cloneNode(true); + audio.numChannels = info.audio.numChannels; + audio.frequency = info.audio.frequency; if (SDL.channelFinished) { audio['onended'] = function() { // TODO: cache these Runtime.getFuncWrapper(SDL.channelFinished, 'vi')(channel); @@ -1426,10 +1491,70 @@ var LibrarySDL = { return (SDL.music.audio && !SDL.music.audio.paused) ? 1 : 0; }, + // http://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer_38.html#SEC38 + // "Note: Does not check if the channel has been paused." + Mix_Playing: function(id) { + if (id === -1) { + var count = 0; + for (var i = 0; i < SDL.audios.length; i++) { + count += SDL.Mix_Playing(i); + } + return count; + } + var info = SDL.audios[id]; + if (info && info.audio && !info.audio.paused) { + return 1; + } + return 0; + }, + + Mix_Pause: function(id) { + if (id === -1) { + for (var i = 0; i<SDL.audios.length;i++) { + SDL.Mix_Pause(i); + } + return; + } + var info = SDL.audios[id]; + if (info && info.audio) { + info.audio.pause(); + } + }, + + // http://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer_39.html#SEC39 + Mix_Paused: function(id) { + if (id === -1) { + var pausedCount = 0; + for (var i = 0; i<SDL.audios.length;i++) { + pausedCount += SDL.Mix_Paused(i); + } + return pausedCount; + } + var info = SDL.audios[id]; + if (info && info.audio && info.audio.paused) { + return 1; + } + return 0; + }, + Mix_PausedMusic: function() { return (SDL.music.audio && SDL.music.audio.paused) ? 1 : 0; }, + // http://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer_33.html#SEC33 + Mix_Resume: function(id) { + if (id === -1) { + for (var i = 0; i<SDL.audios.length;i++) { + SDL.Mix_Resume(i); + } + return; + } + var info = SDL.audios[id]; + if (info && info.audio) { + info.audio.play(); + } + }, + // SDL TTF TTF_Init: function() { return 0 }, diff --git a/src/long.js b/src/long.js index c3b0e605..c3651bd9 100644 --- a/src/long.js +++ b/src/long.js @@ -1551,6 +1551,17 @@ var i64Math = (function() { // Emscripten wrapper HEAP32[tempDoublePtr>>2] = ret.low_; HEAP32[tempDoublePtr+4>>2] = ret.high_; }, + abs: function(l, h) { + var x = new goog.math.Long(l, h); + var ret; + if (x.isNegative()) { + ret = x.negate(); + } else { + ret = x; + } + HEAP32[tempDoublePtr>>2] = ret.low_; + HEAP32[tempDoublePtr+4>>2] = ret.high_; + }, ensureTemps: function() { if (Wrapper.ensuredTemps) return; Wrapper.ensuredTemps = true; diff --git a/src/modules.js b/src/modules.js index 5b48b305..797d4d83 100644 --- a/src/modules.js +++ b/src/modules.js @@ -180,7 +180,16 @@ var Variables = { globals: {}, indexedGlobals: {}, // for indexed globals, ident ==> index // Used in calculation of indexed globals - nextIndexedOffset: 0 + nextIndexedOffset: 0, + + resolveAliasToIdent: function(ident) { + while (1) { + var varData = Variables.globals[ident]; + if (!(varData && varData.targetIdent)) break; + ident = varData.targetIdent; // might need to eval to turn (6) into 6 + } + return ident; + }, }; var Types = { @@ -331,12 +340,23 @@ var Functions = { } } } + if (table.length > 20) { + // add some newlines in the table, for readability + var j = 10; + while (j+10 < table.length) { + table[j] += '\n'; + j += 10; + } + } var indices = table.toString().replace('"', ''); if (BUILD_AS_SHARED_LIB) { // Shared libraries reuse the parent's function table. tables[t] = Functions.getTable(t) + '.push.apply(' + Functions.getTable(t) + ', [' + indices + ']);\n'; } else { tables[t] = 'var ' + Functions.getTable(t) + ' = [' + indices + '];\n'; + if (SAFE_DYNCALLS) { + tables[t] += 'var FUNCTION_TABLE_NAMES = ' + JSON.stringify(table).replace(/\n/g, '').replace(/,0/g, ',0\n') + ';\n'; + } } } if (!generated && !ASM_JS) { diff --git a/src/parseTools.js b/src/parseTools.js index a076c862..48274cd5 100644 --- a/src/parseTools.js +++ b/src/parseTools.js @@ -78,6 +78,7 @@ function toNiceIdent(ident) { assert(ident); if (parseFloat(ident) == ident) return ident; if (ident == 'null') return '0'; // see parseNumerical + if (ident == 'undef') return '0'; return ident.replace('%', '$').replace(/["&\\ \.@:<>,\*\[\]\(\)-]/g, '_'); } @@ -103,6 +104,11 @@ function isNiceIdent(ident, loose) { } } +function isJSVar(ident) { + return /^\(?[$_]?[\w$_\d ]*\)+$/.test(ident); + +} + function isStructPointerType(type) { // This test is necessary for clang - in llvm-gcc, we // could check for %struct. The downside is that %1 can @@ -687,7 +693,7 @@ function makeCopyI64(value) { function parseArbitraryInt(str, bits) { // We parse the string into a vector of digits, base 10. This is convenient to work on. - assert(bits % 32 == 0 || ('i' + (bits % 32)) in Runtime.INT_TYPES, 'Arbitrary-sized ints must tails that are of legal size'); + assert(bits > 0); // NB: we don't check that the value in str can fit in this amount of bits function str2vec(s) { // index 0 is the highest value var ret = []; @@ -977,27 +983,27 @@ function checkSafeHeap() { return SAFE_HEAP === 1 || checkSpecificSafeHeap(); } -if (ASM_JS) { - var hexMemoryMask = '0x' + (TOTAL_MEMORY-1).toString(16); - var decMemoryMask = (TOTAL_MEMORY-1).toString(); - var memoryMask = hexMemoryMask.length <= decMemoryMask.length ? hexMemoryMask : decMemoryMask; -} - function getHeapOffset(offset, type, forceAsm) { if (USE_TYPED_ARRAYS !== 2) { return offset; - } else { - if (Runtime.getNativeFieldSize(type) > 4) { - type = 'i32'; // XXX we emulate 64-bit values as 32 - } - var shifts = Math.log(Runtime.getNativeTypeSize(type))/Math.LN2; - offset = '(' + offset + ')'; - if (ASM_JS && (phase == 'funcs' || forceAsm)) offset = '(' + offset + '&' + memoryMask + ')'; - if (shifts != 0) { - return '(' + offset + '>>' + shifts + ')'; + } + + if (Runtime.getNativeFieldSize(type) > 4) { + type = 'i32'; // XXX we emulate 64-bit values as 32 + } + + var sz = Runtime.getNativeTypeSize(type); + var shifts = Math.log(sz)/Math.LN2; + offset = '(' + offset + ')'; + if (shifts != 0) { + if (CHECK_HEAP_ALIGN) { + return '(CHECK_ALIGN_' + sz + '(' + offset + ')>>' + shifts + ')'; } else { - return offset; + return '(' + offset + '>>' + shifts + ')'; } + } else { + // we need to guard against overflows here, HEAP[U]8 expects a guaranteed int + return isJSVar(offset) ? offset : '(' + offset + '|0)'; } } @@ -1046,20 +1052,6 @@ function asmCoercion(value, type, signedness) { } } -var TWO_TWENTY = Math.pow(2, 20); - -function asmMultiplyI32(a, b) { - // special-case: there is no integer multiply in asm, because there is no true integer - // multiply in JS. While we wait for Math.imul, do double multiply - if ((isNumber(a) && Math.abs(a) < TWO_TWENTY) || (isNumber(b) && Math.abs(b) < TWO_TWENTY)) { - return '(((' + a + ')*(' + b + '))&-1)'; // small enough to emit directly as a multiply - } - if (USE_MATH_IMUL) { - return 'Math.imul(' + a + ',' + b + ')'; - } - return '(~~(+((' + a + ')|0) * +((' + b + ')|0)))'; -} - function asmFloatToInt(x) { return '(~~(' + x + '))'; } @@ -1071,7 +1063,6 @@ function makeGetTempDouble(i, type, forSet) { // get an aliased part of the temp var ptr = getFastValue('tempDoublePtr', '+', Runtime.getNativeTypeSize(type)*i); var offset; if (type == 'double') { - if (ASM_JS) ptr = '(' + ptr + ')&' + memoryMask; offset = '(' + ptr + ')>>3'; } else { offset = getHeapOffset(ptr, type); @@ -1154,8 +1145,8 @@ function makeGetValue(ptr, pos, type, noNeedFirst, unsigned, ignore, align, noSa } } -function makeGetValueAsm(ptr, pos, type) { - return makeGetValue(ptr, pos, type, null, null, null, null, null, true); +function makeGetValueAsm(ptr, pos, type, unsigned) { + return makeGetValue(ptr, pos, type, null, unsigned, null, null, null, true); } function indexizeFunctions(value, type) { @@ -1258,7 +1249,6 @@ function makeSetValueAsm(ptr, pos, value, type, noNeedFirst, ignore, align, noSa return makeSetValue(ptr, pos, value, type, noNeedFirst, ignore, align, noSafe, sep, forcedAlign, true); } -var SEEK_OPTIMAL_ALIGN_MIN = 20; var UNROLL_LOOP_MAX = 8; function makeSetValues(ptr, pos, value, type, num, align) { @@ -1278,7 +1268,7 @@ function makeSetValues(ptr, pos, value, type, num, align) { } else { // USE_TYPED_ARRAYS == 2 // If we don't know how to handle this at compile-time, or handling it is best done in a large amount of code, call memset // TODO: optimize the case of numeric num but non-numeric value - if (!isNumber(num) || !isNumber(value) || (align < 4 && parseInt(num) >= SEEK_OPTIMAL_ALIGN_MIN)) { + if (!isNumber(num) || !isNumber(value) || (parseInt(num)/align >= UNROLL_LOOP_MAX)) { return '_memset(' + asmCoercion(getFastValue(ptr, '+', pos), 'i32') + ', ' + asmCoercion(value, 'i32') + ', ' + asmCoercion(num, 'i32') + ')'; } num = parseInt(num); @@ -1293,13 +1283,7 @@ function makeSetValues(ptr, pos, value, type, num, align) { [4, 2, 1].forEach(function(possibleAlign) { if (num == 0) return; if (align >= possibleAlign) { - if (num <= UNROLL_LOOP_MAX*possibleAlign || ASM_JS) { // XXX test asm performance - ret.push(unroll('i' + (possibleAlign*8), Math.floor(num/possibleAlign), possibleAlign, values[possibleAlign])); - } else { - ret.push('for (var $$dest = ' + getFastValue(ptr, '+', pos) + (possibleAlign > 1 ? '>>' + log2(possibleAlign) : '') + ', ' + - '$$stop = $$dest + ' + Math.floor(num/possibleAlign) + '; $$dest < $$stop; $$dest++) {\n' + - ' HEAP' + (possibleAlign*8) + '[$$dest] = ' + values[possibleAlign] + '\n}'); - } + ret.push(unroll('i' + (possibleAlign*8), Math.floor(num/possibleAlign), possibleAlign, values[possibleAlign])); pos = getFastValue(pos, '+', Math.floor(num/possibleAlign)*possibleAlign); num %= possibleAlign; } @@ -1336,7 +1320,7 @@ function makeCopyValues(dest, src, num, type, modifier, align, sep) { unroll(type, 1) + ' }'; } else { // USE_TYPED_ARRAYS == 2 // If we don't know how to handle this at compile-time, or handling it is best done in a large amount of code, call memset - if (!isNumber(num) || (align < 4 && parseInt(num) >= SEEK_OPTIMAL_ALIGN_MIN)) { + if (!isNumber(num) || (parseInt(num)/align >= UNROLL_LOOP_MAX)) { return '_memcpy(' + dest + ', ' + src + ', ' + num + ')'; } num = parseInt(num); @@ -1344,16 +1328,7 @@ function makeCopyValues(dest, src, num, type, modifier, align, sep) { [4, 2, 1].forEach(function(possibleAlign) { if (num == 0) return; if (align >= possibleAlign) { - // If we can unroll the loop, do so. Also do so if we must unroll it (we do not create real loops when inlined) - if (num <= UNROLL_LOOP_MAX*possibleAlign || sep == ',' || ASM_JS) { // XXX test asm performance - ret.push(unroll('i' + (possibleAlign*8), Math.floor(num/possibleAlign), possibleAlign)); - } else { - assert(sep == ';'); - ret.push('for (var $$src = ' + src + (possibleAlign > 1 ? '>>' + log2(possibleAlign) : '') + ', ' + - '$$dest = ' + dest + (possibleAlign > 1 ? '>>' + log2(possibleAlign) : '') + ', ' + - '$$stop = $$src + ' + Math.floor(num/possibleAlign) + '; $$src < $$stop; $$src++, $$dest++) {\n' + - ' HEAP' + (possibleAlign*8) + '[$$dest] = HEAP' + (possibleAlign*8) + '[$$src]\n}'); - } + ret.push(unroll('i' + (possibleAlign*8), Math.floor(num/possibleAlign), possibleAlign)); src = getFastValue(src, '+', Math.floor(num/possibleAlign)*possibleAlign); dest = getFastValue(dest, '+', Math.floor(num/possibleAlign)*possibleAlign); num %= possibleAlign; @@ -1373,17 +1348,23 @@ function makeHEAPView(which, start, end) { var PLUS_MUL = set('+', '*'); var MUL_DIV = set('*', '/'); var PLUS_MINUS = set('+', '-'); +var TWO_TWENTY = Math.pow(2, 20); // Given two values and an operation, returns the result of that operation. // Tries to do as much as possible at compile time. +// Leaves overflows etc. unhandled, *except* for integer multiply, in order to be efficient with Math.imul function getFastValue(a, op, b, type) { a = a.toString(); b = b.toString(); + a = a == 'true' ? '1' : (a == 'false' ? '0' : a); + b = b == 'true' ? '1' : (b == 'false' ? '0' : b); if (isNumber(a) && isNumber(b)) { if (op == 'pow') { return Math.pow(a, b).toString(); } else { - return eval(a + op + '(' + b + ')').toString(); // parens protect us from "5 - -12" being seen as "5--12" which is "(5--)12" + var value = eval(a + op + '(' + b + ')'); // parens protect us from "5 - -12" being seen as "5--12" which is "(5--)12" + if (op == '/' && type in Runtime.INT_TYPES) value = value|0; // avoid emitting floats + return value.toString(); } } if (op == 'pow') { @@ -1411,8 +1392,14 @@ function getFastValue(a, op, b, type) { return '(' + a + '<<' + shifts + ')'; } } - if (ASM_JS && !(type in Runtime.FLOAT_TYPES)) { - return asmMultiplyI32(a, b); // unoptimized multiply, do it using asm.js's special multiply operation + if (!(type in Runtime.FLOAT_TYPES)) { + // if guaranteed small enough to not overflow into a double, do a normal multiply + var bits = getBits(type) || 32; // default is 32-bit multiply for things like getelementptr indexes + // Note that we can emit simple multiple in non-asm.js mode, but asm.js will not parse "16-bit" multiple, so must do imul there + if ((isNumber(a) && Math.abs(a) < TWO_TWENTY) || (isNumber(b) && Math.abs(b) < TWO_TWENTY) || (bits < 32 && !ASM_JS)) { + return '(((' + a + ')*(' + b + '))&' + ((Math.pow(2, bits)-1)|0) + ')'; // keep a non-eliminatable coercion directly on this + } + return 'Math.imul(' + a + ',' + b + ')'; } } else { if (a == '0') { @@ -1557,7 +1544,7 @@ function makePointer(slab, pos, allocator, type, ptr) { var ret = ''; var index = 0; while (index < array.length) { - ret = (ret ? ret + '.concat(' : '') + '[' + array.slice(index, index + chunkSize).map(JSON.stringify) + ']' + (ret ? ')' : ''); + ret = (ret ? ret + '.concat(' : '') + '[' + array.slice(index, index + chunkSize).map(JSON.stringify) + ']' + (ret ? ')\n' : ''); index += chunkSize; } return ret; @@ -1718,9 +1705,7 @@ function handleOverflow(text, bits) { if (!bits) return text; var correct = correctOverflows(); warnOnce(!correct || bits <= 32, 'Cannot correct overflows of this many bits: ' + bits); - if (CHECK_OVERFLOWS) return 'CHECK_OVERFLOW(' + text + ', ' + bits + ', ' + Math.floor(correctSpecificOverflow() && !PGO) + ( - PGO ? ', "' + Debugging.getIdentifier() + '"' : '' - ) + ')'; + if (CHECK_OVERFLOWS) return 'CHECK_OVERFLOW(' + text + ', ' + bits + ', ' + Math.floor(correctSpecificOverflow()) + ')'; if (!correct) return text; if (bits == 32) { return '((' + text + ')|0)'; @@ -1828,9 +1813,7 @@ function makeSignOp(value, type, op, force, ignore) { var bits, full; if (type in Runtime.INT_TYPES) { bits = parseInt(type.substr(1)); - full = op + 'Sign(' + value + ', ' + bits + ', ' + Math.floor(ignore || (correctSpecificSign() && !PGO)) + ( - PGO ? ', "' + (ignore ? '' : Debugging.getIdentifier()) + '"' : '' - ) + ')'; + full = op + 'Sign(' + value + ', ' + bits + ', ' + Math.floor(ignore || (correctSpecificSign())) + ')'; // Always sign/unsign constants at compile time, regardless of CHECK/CORRECT if (isNumber(value)) { return eval(full).toString(); @@ -1842,9 +1825,10 @@ function makeSignOp(value, type, op, force, ignore) { if (!CHECK_SIGNS || ignore) { if (bits === 32) { if (op === 're') { - return '((' + value + ')|0)'; + return '(' + getFastValue(value, '|', '0') + ')'; } else { - return '((' + value + ')>>>0)'; + + return '(' + getFastValue(value, '>>>', '0') + ')'; // Alternatively, we can consider the lengthier // return makeInlineCalculation('VALUE >= 0 ? VALUE : ' + Math.pow(2, bits) + ' + VALUE', value, 'tempBigInt'); // which does not always turn us into a 32-bit *un*signed value @@ -1853,7 +1837,7 @@ function makeSignOp(value, type, op, force, ignore) { if (op === 're') { return makeInlineCalculation('(VALUE << ' + (32-bits) + ') >> ' + (32-bits), value, 'tempInt'); } else { - return '((' + value + ')&' + (Math.pow(2, bits)-1) + ')'; + return '(' + getFastValue(value, '&', Math.pow(2, bits)-1) + ')'; } } else { // bits > 32 if (op === 're') { @@ -2143,14 +2127,7 @@ function processMathop(item) { case 'add': return handleOverflow(getFastValue(idents[0], '+', idents[1], item.type), bits); case 'sub': return handleOverflow(getFastValue(idents[0], '-', idents[1], item.type), bits); case 'sdiv': case 'udiv': return makeRounding(getFastValue(idents[0], '/', idents[1], item.type), bits, op[0] === 's'); - case 'mul': { - if (bits == 32 && PRECISE_I32_MUL) { - Types.preciseI64MathUsed = true; - return '(i64Math' + (ASM_JS ? '_' : '.') + 'multiply(' + asmCoercion(idents[0], 'i32') + ',0,' + asmCoercion(idents[1], 'i32') + ',0),' + makeGetValue('tempDoublePtr', 0, 'i32') + ')'; - } else { - return '((' +getFastValue(idents[0], '*', idents[1], item.type) + ')&-1)'; // force a non-eliminatable coercion here, to prevent a double result from leaking - } - } + case 'mul': return getFastValue(idents[0], '*', idents[1], item.type); // overflow handling is already done in getFastValue for '*' case 'urem': case 'srem': return getFastValue(idents[0], '%', idents[1], item.type); case 'or': { if (bits > 32) { @@ -2211,7 +2188,6 @@ function processMathop(item) { case 'ne': case 'eq': { // We must sign them, so we do not compare -1 to 255 (could have unsigned them both too) // since LLVM tells us if <=, >= etc. comparisons are signed, but not == and !=. - assert(paramTypes[0] == paramTypes[1]); idents[0] = makeSignOp(idents[0], paramTypes[0], 're'); idents[1] = makeSignOp(idents[1], paramTypes[1], 're'); return idents[0] + (variant === 'eq' ? '==' : '!=') + idents[1]; diff --git a/src/preamble.js b/src/preamble.js index aab50e9a..9bc68d8f 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -150,51 +150,24 @@ function SAFE_HEAP_COPY_HISTORY(dest, src) { //========================================== #endif -var CorrectionsMonitor = { -#if PGO - MAX_ALLOWED: Infinity, -#else - MAX_ALLOWED: 0, // XXX +#if CHECK_HEAP_ALIGN +//======================================== +// Debugging tools - alignment check +//======================================== +function CHECK_ALIGN_8(addr) { + assert((addr & 7) == 0, "address must be 8-byte aligned, is " + addr + "!"); + return addr; +} +function CHECK_ALIGN_4(addr) { + assert((addr & 3) == 0, "address must be 4-byte aligned, is " + addr + "!"); + return addr; +} +function CHECK_ALIGN_2(addr) { + assert((addr & 1) == 0, "address must be 2-byte aligned!"); + return addr; +} #endif - corrections: 0, - sigs: {}, - note: function(type, succeed, sig) { - if (!succeed) { - this.corrections++; - if (this.corrections >= this.MAX_ALLOWED) abort('\n\nToo many corrections!'); - } -#if PGO - if (!sig) - sig = (new Error().stack).toString().split('\n')[2].split(':').slice(-1)[0]; // Spidermonkey-specific FIXME - sig = type + '|' + sig; - if (!this.sigs[sig]) { - //Module.print('Correction: ' + sig); - this.sigs[sig] = [0, 0]; // fail, succeed - } - this.sigs[sig][succeed ? 1 : 0]++; -#endif - }, - - print: function() { -#if PGO - var items = []; - for (var sig in this.sigs) { - items.push({ - sig: sig, - fails: this.sigs[sig][0], - succeeds: this.sigs[sig][1], - total: this.sigs[sig][0] + this.sigs[sig][1] - }); - } - items.sort(function(x, y) { return y.total - x.total; }); - for (var i = 0; i < items.length; i++) { - var item = items[i]; - Module.print(item.sig + ' : ' + item.total + ' hits, %' + (Math.ceil(100*item.fails/item.total)) + ' failures'); - } -#endif - } -}; #if CHECK_OVERFLOWS //======================================== @@ -207,24 +180,20 @@ function CHECK_OVERFLOW(value, bits, ignore, sig) { // For signedness issue here, see settings.js, CHECK_SIGNED_OVERFLOWS #if CHECK_SIGNED_OVERFLOWS if (value === Infinity || value === -Infinity || value >= twopbits1 || value < -twopbits1) { - CorrectionsMonitor.note('SignedOverflow', 0, sig); - if (value === Infinity || value === -Infinity || Math.abs(value) >= twopbits) CorrectionsMonitor.note('Overflow'); + throw 'SignedOverflow'; + if (value === Infinity || value === -Infinity || Math.abs(value) >= twopbits) throw 'Overflow'; + } #else if (value === Infinity || value === -Infinity || Math.abs(value) >= twopbits) { - CorrectionsMonitor.note('Overflow', 0, sig); + throw 'Overflow'; + } #endif #if CORRECT_OVERFLOWS - // Fail on >32 bits - we warned at compile time - if (bits <= 32) { - value = value & (twopbits - 1); - } -#endif - } else { -#if CHECK_SIGNED_OVERFLOWS - CorrectionsMonitor.note('SignedOverflow', 1, sig); -#endif - CorrectionsMonitor.note('Overflow', 1, sig); + // Fail on >32 bits - we warned at compile time + if (bits <= 32) { + value = value & (twopbits - 1); } +#endif return value; } #endif @@ -243,39 +212,6 @@ var INDENT = ''; var START_TIME = Date.now(); #endif -#if PROFILE -var PROFILING = 0; -var PROFILING_ROOT = { time: 0, children: {}, calls: 0 }; -var PROFILING_NODE; - -function startProfiling() { - PROFILING_NODE = PROFILING_ROOT; - PROFILING = 1; -} -Module['startProfiling'] = startProfiling; - -function stopProfiling() { - PROFILING = 0; - assert(PROFILING_NODE === PROFILING_ROOT, 'Must have popped all the profiling call stack'); -} -Module['stopProfiling'] = stopProfiling; - -function printProfiling() { - function dumpData(name_, node, indent) { - Module.print(indent + ('________' + node.time).substr(-8) + ': ' + name_ + ' (' + node.calls + ')'); - var children = []; - for (var child in node.children) { - children.push(node.children[child]); - children[children.length-1].name_ = child; - } - children.sort(function(x, y) { return y.time - x.time }); - children.forEach(function(child) { dumpData(child.name_, child, indent + ' ') }); - } - dumpData('root', PROFILING_ROOT, ' '); -} -Module['printProfiling'] = printProfiling; -#endif - //======================================== // Runtime essentials //======================================== @@ -334,11 +270,9 @@ Module["ccall"] = ccall; // Returns the C function with a specified identifier (for C++, you need to do manual name mangling) function getCFunc(ident) { try { - var func = eval('_' + ident); + var func = globalScope['Module']['_' + ident]; // closure exported function + if (!func) func = eval('_' + ident); // explicit lookup } catch(e) { - try { - func = globalScope['Module']['_' + ident]; // closure exported function - } catch(e) {} } assert(func, 'Cannot call unknown function ' + ident + ' (perhaps LLVM optimizations or closure removed it?)'); return func; @@ -478,14 +412,6 @@ Module['ALLOC_STACK'] = ALLOC_STACK; Module['ALLOC_STATIC'] = ALLOC_STATIC; Module['ALLOC_NONE'] = ALLOC_NONE; -// Simple unoptimized memset - necessary during startup -var _memset = function(ptr, value, num) { - var stop = ptr + num; - while (ptr < stop) { - {{{ makeSetValue('ptr++', 0, 'value', 'i8', null, true) }}}; - } -} - // allocate(): This is for internal use. You can use it yourself as well, but the interface // is a little tricky (see docs right below). The reason is that it is optimized // for multiple syntaxes to save space in generated code. So you should @@ -519,7 +445,18 @@ function allocate(slab, types, allocator, ptr) { } if (zeroinit) { - _memset(ret, 0, size); + var ptr = ret, stop; +#if USE_TYPED_ARRAYS == 2 + assert((ret & 3) == 0); + stop = ret + (size & ~3); + for (; ptr < stop; ptr += 4) { + {{{ makeSetValue('ptr', '0', '0', 'i32', null, true) }}}; + } +#endif + stop = ret + size; + while (ptr < stop) { + {{{ makeSetValue('ptr++', '0', '0', 'i8', null, true) }}}; + } return ret; } @@ -530,7 +467,7 @@ function allocate(slab, types, allocator, ptr) { } #endif - var i = 0, type; + var i = 0, type, typeSize, previousType; while (i < size) { var curr = slab[i]; @@ -552,7 +489,13 @@ function allocate(slab, types, allocator, ptr) { #endif setValue(ret+i, curr, type); - i += Runtime.getNativeTypeSize(type); + + // no need to look up size unless type changes, so cache it + if (previousType !== type) { + typeSize = Runtime.getNativeTypeSize(type); + previousType = type; + } + i += typeSize; } return ret; @@ -626,6 +569,7 @@ function enlargeMemory() { while (TOTAL_MEMORY <= STATICTOP) { // Simple heuristic. Override enlargeMemory() if your program has something more optimal for it TOTAL_MEMORY = alignMemoryPage(2*TOTAL_MEMORY); } + assert(TOTAL_MEMORY <= Math.pow(2, 30)); // 2^30==1GB is a practical maximum - 2^31 is already close to possible negative numbers etc. #if USE_TYPED_ARRAYS == 1 var oldIHEAP = IHEAP; Module['HEAP'] = Module['IHEAP'] = HEAP = IHEAP = new Int32Array(TOTAL_MEMORY); @@ -655,47 +599,43 @@ function enlargeMemory() { #endif var TOTAL_STACK = Module['TOTAL_STACK'] || {{{ TOTAL_STACK }}}; -#if ASM_JS == 0 var TOTAL_MEMORY = Module['TOTAL_MEMORY'] || {{{ TOTAL_MEMORY }}}; -#else -var TOTAL_MEMORY = {{{ TOTAL_MEMORY }}}; // in asm, we hardcode the mask, so cannot adjust memory at runtime -#endif var FAST_MEMORY = Module['FAST_MEMORY'] || {{{ FAST_MEMORY }}}; // Initialize the runtime's memory #if USE_TYPED_ARRAYS // check for full engine support (use string 'subarray' to avoid closure compiler confusion) - assert(!!Int32Array && !!Float64Array && !!(new Int32Array(1)['subarray']) && !!(new Int32Array(1)['set']), - 'Cannot fallback to non-typed array case: Code is too specialized'); +assert(!!Int32Array && !!Float64Array && !!(new Int32Array(1)['subarray']) && !!(new Int32Array(1)['set']), + 'Cannot fallback to non-typed array case: Code is too specialized'); #if USE_TYPED_ARRAYS == 1 - HEAP = IHEAP = new Int32Array(TOTAL_MEMORY); - IHEAPU = new Uint32Array(IHEAP.buffer); +HEAP = IHEAP = new Int32Array(TOTAL_MEMORY); +IHEAPU = new Uint32Array(IHEAP.buffer); #if USE_FHEAP - FHEAP = new Float64Array(TOTAL_MEMORY); +FHEAP = new Float64Array(TOTAL_MEMORY); #endif #endif #if USE_TYPED_ARRAYS == 2 - var buffer = new ArrayBuffer(TOTAL_MEMORY); - HEAP8 = new Int8Array(buffer); - HEAP16 = new Int16Array(buffer); - HEAP32 = new Int32Array(buffer); - HEAPU8 = new Uint8Array(buffer); - HEAPU16 = new Uint16Array(buffer); - HEAPU32 = new Uint32Array(buffer); - HEAPF32 = new Float32Array(buffer); - HEAPF64 = new Float64Array(buffer); - - // Endianness check (note: assumes compiler arch was little-endian) - HEAP32[0] = 255; - assert(HEAPU8[0] === 255 && HEAPU8[3] === 0, 'Typed arrays 2 must be run on a little-endian system'); +var buffer = new ArrayBuffer(TOTAL_MEMORY); +HEAP8 = new Int8Array(buffer); +HEAP16 = new Int16Array(buffer); +HEAP32 = new Int32Array(buffer); +HEAPU8 = new Uint8Array(buffer); +HEAPU16 = new Uint16Array(buffer); +HEAPU32 = new Uint32Array(buffer); +HEAPF32 = new Float32Array(buffer); +HEAPF64 = new Float64Array(buffer); + +// Endianness check (note: assumes compiler arch was little-endian) +HEAP32[0] = 255; +assert(HEAPU8[0] === 255 && HEAPU8[3] === 0, 'Typed arrays 2 must be run on a little-endian system'); #endif #else - // Make sure that our HEAP is implemented as a flat array. - HEAP = []; // Hinting at the size with |new Array(TOTAL_MEMORY)| should help in theory but makes v8 much slower - for (var i = 0; i < FAST_MEMORY; i++) { - HEAP[i] = 0; // XXX We do *not* use {{| makeSetValue(0, 'i', 0, 'null') |}} here, since this is done just to optimize runtime speed - } +// Make sure that our HEAP is implemented as a flat array. +HEAP = []; // Hinting at the size with |new Array(TOTAL_MEMORY)| should help in theory but makes v8 much slower +for (var i = 0; i < FAST_MEMORY; i++) { + HEAP[i] = 0; // XXX We do *not* use {{| makeSetValue(0, 'i', 0, 'null') |}} here, since this is done just to optimize runtime speed +} #endif Module['HEAP'] = HEAP; @@ -722,7 +662,7 @@ STACK_MAX = TOTAL_STACK; // we lose a little stack here, but TOTAL_STACK is nice #if USE_TYPED_ARRAYS == 2 var tempDoublePtr = Runtime.alignMemory(allocate(12, 'i8', ALLOC_STACK), 8); assert(tempDoublePtr % 8 == 0); -function copyTempFloat(ptr) { // functions, because inlining this code is increases code size too much +function copyTempFloat(ptr) { // functions, because inlining this code increases code size too much HEAP8[tempDoublePtr] = HEAP8[ptr]; HEAP8[tempDoublePtr+1] = HEAP8[ptr+1]; HEAP8[tempDoublePtr+2] = HEAP8[ptr+2]; @@ -773,9 +713,6 @@ function preMain() { } function exitRuntime() { callRuntimeCallbacks(__ATEXIT__); - - // Print summary of correction activity - CorrectionsMonitor.print(); } // Tools @@ -832,6 +769,20 @@ Module['writeArrayToMemory'] = writeArrayToMemory; {{{ unSign }}} {{{ reSign }}} +#if PRECISE_I32_MUL +if (!Math.imul) Math.imul = function(a, b) { + var ah = a >>> 16; + var al = a & 0xffff; + var bh = b >>> 16; + var bl = b & 0xffff; + return (al*bl + ((ah*bl + al*bh) << 16))|0; +}; +#else +Math.imul = function(a, b) { + return (a*b)|0; // fast but imprecise +}; +#endif + // A counter of dependencies for calling run(). If we need to // do asynchronous work before running, increment this and // decrement it. Incrementing must happen in a place like diff --git a/src/relooper/Relooper.cpp b/src/relooper/Relooper.cpp index ae8577b1..ae393de3 100644 --- a/src/relooper/Relooper.cpp +++ b/src/relooper/Relooper.cpp @@ -347,17 +347,25 @@ struct RelooperRecursor { RelooperRecursor(Relooper *ParentInit) : Parent(ParentInit) {} }; +typedef std::list<Block*> BlockList; + void Relooper::Calculate(Block *Entry) { // Scan and optimize the input struct PreOptimizer : public RelooperRecursor { PreOptimizer(Relooper *Parent) : RelooperRecursor(Parent) {} BlockSet Live; - void FindLive(Block *Curr) { - if (Live.find(Curr) != Live.end()) return; - Live.insert(Curr); - for (BlockBranchMap::iterator iter = Curr->BranchesOut.begin(); iter != Curr->BranchesOut.end(); iter++) { - FindLive(iter->first); + void FindLive(Block *Root) { + BlockList ToInvestigate; + ToInvestigate.push_back(Root); + while (ToInvestigate.size() > 0) { + Block *Curr = ToInvestigate.front(); + ToInvestigate.pop_front(); + if (Live.find(Curr) != Live.end()) continue; + Live.insert(Curr); + for (BlockBranchMap::iterator iter = Curr->BranchesOut.begin(); iter != Curr->BranchesOut.end(); iter++) { + ToInvestigate.push_back(iter->first); + } } } @@ -529,7 +537,6 @@ void Relooper::Calculate(Block *Entry) { // ignore directly reaching the entry itself by another entry. void FindIndependentGroups(BlockSet &Blocks, BlockSet &Entries, BlockBlockSetMap& IndependentGroups) { typedef std::map<Block*, Block*> BlockBlockMap; - typedef std::list<Block*> BlockList; struct HelperClass { BlockBlockSetMap& IndependentGroups; @@ -872,33 +879,38 @@ void Relooper::Calculate(Block *Entry) { // A flow operation is trivially unneeded if the shape we naturally get to by normal code // execution is the same as the flow forces us to. void RemoveUnneededFlows(Shape *Root, Shape *Natural=NULL) { - SHAPE_SWITCH(Root, { - // If there is a next block, we already know at Simple creation time to make direct branches, - // and we can do nothing more. If there is no next however, then Natural is where we will - // go to by doing nothing, so we can potentially optimize some branches to direct. - if (Simple->Next) { - RemoveUnneededFlows(Simple->Next, Natural); - } else { - for (BlockBranchMap::iterator iter = Simple->Inner->ProcessedBranchesOut.begin(); iter != Simple->Inner->ProcessedBranchesOut.end(); iter++) { - Block *Target = iter->first; - Branch *Details = iter->second; - if (Details->Type != Branch::Direct && Target->Parent == Natural) { - Details->Type = Branch::Direct; - if (MultipleShape *Multiple = Shape::IsMultiple(Details->Ancestor)) { - Multiple->NeedLoop--; + Shape *Next = Root; + while (Next) { + Root = Next; + Next = NULL; + SHAPE_SWITCH(Root, { + // If there is a next block, we already know at Simple creation time to make direct branches, + // and we can do nothing more. If there is no next however, then Natural is where we will + // go to by doing nothing, so we can potentially optimize some branches to direct. + if (Simple->Next) { + Next = Simple->Next; + } else { + for (BlockBranchMap::iterator iter = Simple->Inner->ProcessedBranchesOut.begin(); iter != Simple->Inner->ProcessedBranchesOut.end(); iter++) { + Block *Target = iter->first; + Branch *Details = iter->second; + if (Details->Type != Branch::Direct && Target->Parent == Natural) { + Details->Type = Branch::Direct; + if (MultipleShape *Multiple = Shape::IsMultiple(Details->Ancestor)) { + Multiple->NeedLoop--; + } } } } - } - }, { - for (BlockShapeMap::iterator iter = Multiple->InnerMap.begin(); iter != Multiple->InnerMap.end(); iter++) { - RemoveUnneededFlows(iter->second, Multiple->Next); - } - RemoveUnneededFlows(Multiple->Next, Natural); - }, { - RemoveUnneededFlows(Loop->Inner, Loop->Inner); - RemoveUnneededFlows(Loop->Next, Natural); - }); + }, { + for (BlockShapeMap::iterator iter = Multiple->InnerMap.begin(); iter != Multiple->InnerMap.end(); iter++) { + RemoveUnneededFlows(iter->second, Multiple->Next); + } + Next = Multiple->Next; + }, { + RemoveUnneededFlows(Loop->Inner, Loop->Inner); + Next = Loop->Next; + }); + } } // After we know which loops exist, we can calculate which need to be labeled @@ -909,48 +921,54 @@ void Relooper::Calculate(Block *Entry) { } std::stack<Shape*> &LoopStack = *((std::stack<Shape*>*)Closure); - SHAPE_SWITCH(Root, { - MultipleShape *Fused = Shape::IsMultiple(Root->Next); - // If we are fusing a Multiple with a loop into this Simple, then visit it now - if (Fused && Fused->NeedLoop) { - LoopStack.push(Fused); - RECURSE_MULTIPLE_MANUAL(FindLabeledLoops, Fused); - } - for (BlockBranchMap::iterator iter = Simple->Inner->ProcessedBranchesOut.begin(); iter != Simple->Inner->ProcessedBranchesOut.end(); iter++) { - Block *Target = iter->first; - Branch *Details = iter->second; - if (Details->Type != Branch::Direct) { - assert(LoopStack.size() > 0); - if (Details->Ancestor != LoopStack.top()) { - LabeledShape *Labeled = Shape::IsLabeled(Details->Ancestor); - Labeled->Labeled = true; - Details->Labeled = true; - } else { - Details->Labeled = false; + Shape *Next = Root; + while (Next) { + Root = Next; + Next = NULL; + + SHAPE_SWITCH(Root, { + MultipleShape *Fused = Shape::IsMultiple(Root->Next); + // If we are fusing a Multiple with a loop into this Simple, then visit it now + if (Fused && Fused->NeedLoop) { + LoopStack.push(Fused); + RECURSE_MULTIPLE_MANUAL(FindLabeledLoops, Fused); + } + for (BlockBranchMap::iterator iter = Simple->Inner->ProcessedBranchesOut.begin(); iter != Simple->Inner->ProcessedBranchesOut.end(); iter++) { + Block *Target = iter->first; + Branch *Details = iter->second; + if (Details->Type != Branch::Direct) { + assert(LoopStack.size() > 0); + if (Details->Ancestor != LoopStack.top()) { + LabeledShape *Labeled = Shape::IsLabeled(Details->Ancestor); + Labeled->Labeled = true; + Details->Labeled = true; + } else { + Details->Labeled = false; + } } } - } - if (Fused && Fused->NeedLoop) { - LoopStack.pop(); - if (Fused->Next) FindLabeledLoops(Fused->Next); - } else { - if (Root->Next) FindLabeledLoops(Root->Next); - } - }, { - if (Multiple->NeedLoop) { - LoopStack.push(Multiple); - } - RECURSE_MULTIPLE(FindLabeledLoops); - if (Multiple->NeedLoop) { + if (Fused && Fused->NeedLoop) { + LoopStack.pop(); + Next = Fused->Next; + } else { + Next = Root->Next; + } + }, { + if (Multiple->NeedLoop) { + LoopStack.push(Multiple); + } + RECURSE_MULTIPLE(FindLabeledLoops); + if (Multiple->NeedLoop) { + LoopStack.pop(); + } + Next = Root->Next; + }, { + LoopStack.push(Loop); + RECURSE_LOOP(FindLabeledLoops); LoopStack.pop(); - } - if (Root->Next) FindLabeledLoops(Root->Next); - }, { - LoopStack.push(Loop); - RECURSE_LOOP(FindLabeledLoops); - LoopStack.pop(); - if (Root->Next) FindLabeledLoops(Root->Next); - }); + Next = Root->Next; + }); + } if (First) { delete (std::stack<Shape*>*)Closure; diff --git a/src/relooper/fuzzer.py b/src/relooper/fuzzer.py index 887eab3b..96929028 100644 --- a/src/relooper/fuzzer.py +++ b/src/relooper/fuzzer.py @@ -98,14 +98,14 @@ int main() { open('fuzz.slow.js', 'w').write(slow) open('fuzz.cpp', 'w').write(fast) print '_' - slow_out = subprocess.Popen(['/home/alon/Dev/mozilla-central/js/src/fast/js', '-m', '-n', 'fuzz.slow.js'], stdout=subprocess.PIPE).communicate()[0] + slow_out = subprocess.Popen(['/home/alon/Dev/odinmonkey/js/src/fast/js', '-m', '-n', 'fuzz.slow.js'], stdout=subprocess.PIPE).communicate()[0] print '.' subprocess.call(['g++', 'fuzz.cpp', 'Relooper.o', '-o', 'fuzz', '-g']) print '*' subprocess.call(['./fuzz'], stdout=open('fuzz.fast.js', 'w')) print '-' - fast_out = subprocess.Popen(['/home/alon/Dev/mozilla-central/js/src/fast/js', '-m', '-n', 'fuzz.fast.js'], stdout=subprocess.PIPE).communicate()[0] + fast_out = subprocess.Popen(['/home/alon/Dev/odinmonkey/js/src/fast/js', '-m', '-n', 'fuzz.fast.js'], stdout=subprocess.PIPE).communicate()[0] print if slow_out != fast_out: diff --git a/src/relooper/test2.txt b/src/relooper/test2.txt index a847e806..c77ce491 100644 --- a/src/relooper/test2.txt +++ b/src/relooper/test2.txt @@ -1,11 +1,12 @@ ep -L1: -if (ep -> LBB1) { - LBB1 - if (!(LBB1 -> LBB2)) { - break L1; +do { + if (ep -> LBB1) { + LBB1 + if (!(LBB1 -> LBB2)) { + break; + } + LBB2 } - LBB2 -} +} while(0); LBB3 diff --git a/src/relooper/test3.txt b/src/relooper/test3.txt index 7d06f06a..696542ef 100644 --- a/src/relooper/test3.txt +++ b/src/relooper/test3.txt @@ -1,25 +1,27 @@ ep -L1: -if (ep -> LBB1) { - LBB1 - if (!(LBB1 -> LBB2)) { - break L1; +do { + if (ep -> LBB1) { + LBB1 + if (!(LBB1 -> LBB2)) { + break; + } + LBB2 } - LBB2 -} +} while(0); LBB3 -L5: -if (LBB3 -> LBB4) { - LBB4 - if (!(LBB4 -> LBB5)) { - break L5; - } - while(1) { - LBB5 - if (LBB5 -> LBB6) { - break L5; +L5: do { + if (LBB3 -> LBB4) { + LBB4 + if (!(LBB4 -> LBB5)) { + break; + } + while(1) { + LBB5 + if (LBB5 -> LBB6) { + break L5; + } } } -} +} while(0); LBB6 diff --git a/src/relooper/test4.txt b/src/relooper/test4.txt index 2ab3265a..f0bfb972 100644 --- a/src/relooper/test4.txt +++ b/src/relooper/test4.txt @@ -1,16 +1,17 @@ //19 -L1: -if ( 1 ) { - //20 - if (!( 1 )) { +do { + if ( 1 ) { + //20 + if (!( 1 )) { + label = 4; + break; + } + //21 + break; + } else { label = 4; - break L1; } - //21 - break L1; -} else { - label = 4; -} +} while(0); if (label == 4) { //22 } diff --git a/src/relooper/test6.txt b/src/relooper/test6.txt index 0ec7e666..c5effd08 100644 --- a/src/relooper/test6.txt +++ b/src/relooper/test6.txt @@ -1,11 +1,12 @@ //0 -L1: -if (check(0)) { - //1 - if (!(check(1))) { - break L1; +do { + if (check(0)) { + //1 + if (!(check(1))) { + break; + } + //2 } - //2 -} +} while(0); //3 diff --git a/src/relooper/test_debug.txt b/src/relooper/test_debug.txt index 02377fb7..1c7d0508 100644 --- a/src/relooper/test_debug.txt +++ b/src/relooper/test_debug.txt @@ -83,13 +83,14 @@ int main() { // === Optimizing shapes === // Fusing Multiple to Simple ep -L1: -if (ep -> LBB1) { - LBB1 - if (!(LBB1 -> LBB2)) { - break L1; +do { + if (ep -> LBB1) { + LBB1 + if (!(LBB1 -> LBB2)) { + break; + } + LBB2 } - LBB2 -} +} while(0); LBB3 diff --git a/src/relooper/test_fuzz1.txt b/src/relooper/test_fuzz1.txt index 09edb594..5122257e 100644 --- a/src/relooper/test_fuzz1.txt +++ b/src/relooper/test_fuzz1.txt @@ -3,12 +3,13 @@ print('entry'); var label; var state; var decisions = [4, 1, 7, 2, 6, 6, 8]; var index = 0; function check() { if (index == decisions.length) throw 'HALT'; return decisions[index++] } print(5); state = check(); print(6); state = check(); -L3: -if (state == 7) { - print(7); state = check(); - label = 3; - break L3; -} +do { + if (state == 7) { + print(7); state = check(); + label = 3; + break; + } +} while(0); L5: while(1) { if (label == 3) { label = 0; diff --git a/src/relooper/test_fuzz5.txt b/src/relooper/test_fuzz5.txt index 7c795d53..9548205c 100644 --- a/src/relooper/test_fuzz5.txt +++ b/src/relooper/test_fuzz5.txt @@ -3,21 +3,22 @@ print('entry'); var label; var state; var decisions = [133, 98, 134, 143, 162, 187, 130, 87, 91, 49, 102, 47, 9, 132, 179, 176, 157, 25, 64, 161, 57, 107, 16, 167, 185, 45, 191, 180, 23, 131]; var index = 0; function check() { if (index == decisions.length) throw 'HALT'; return decisions[index++] } L1: while(1) { print(7); state = check(); - L3: - if (state % 3 == 1) { - label = 3; - } else if (state % 3 == 0) { - print(8); state = check(); - if (state % 2 == 0) { - label = 5; - break L3; + do { + if (state % 3 == 1) { + label = 3; + } else if (state % 3 == 0) { + print(8); state = check(); + if (state % 2 == 0) { + label = 5; + break; + } else { + label = 7; + break; + } } else { - label = 7; - break L3; + break L1; } - } else { - break L1; - } + } while(0); while(1) { if (label == 3) { label = 0; diff --git a/src/relooper/test_inf.txt b/src/relooper/test_inf.txt index 3e292433..379d2083 100644 --- a/src/relooper/test_inf.txt +++ b/src/relooper/test_inf.txt @@ -5,34 +5,35 @@ if (uint(i4) >= uint(i5)) { code 1 } code 3 -L5: -if (!(i2 == 0)) { - code 4 - while(1) { - code 5 - if (uint(i6) >= uint(i7)) { - code 7 - } else { - code 6 - } - code 8 - if (uint(i6) >= uint(i7)) { - code 10 - } else { - code 9 - } - code 11 - if (uint(i5) >= uint(i6)) { - code 13 - } else { - code 12 - } - code 14 - if (!(i2 != 0)) { - break L5; +L5: do { + if (!(i2 == 0)) { + code 4 + while(1) { + code 5 + if (uint(i6) >= uint(i7)) { + code 7 + } else { + code 6 + } + code 8 + if (uint(i6) >= uint(i7)) { + code 10 + } else { + code 9 + } + code 11 + if (uint(i5) >= uint(i6)) { + code 13 + } else { + code 12 + } + code 14 + if (!(i2 != 0)) { + break L5; + } } } -} +} while(0); code 15 if (uint(i4) >= uint(i5)) { code 17 @@ -40,178 +41,179 @@ if (uint(i4) >= uint(i5)) { code 16 } code 18 -L26: -if (!(i2 == 0)) { - code 19 - while(1) { - code 20 - if (uint(i5) >= uint(i6)) { - code 22 - } else { - code 21 - } - code 23 - if (uint(i5) >= uint(i6)) { - code 25 - } else { - code 24 - } - code 26 - if (uint(i5) >= uint(i6)) { - code 28 - } else { - code 27 - } - code 29 - if (uint(i5) >= uint(i6)) { - code 31 - } else { - code 30 - } - code 32 - if (uint(i5) >= uint(i6)) { - code 34 - } else { - code 33 - } - code 35 - if (uint(i5) >= uint(i6)) { - code 37 - } else { - code 36 - } - code 38 - if (uint(i5) >= uint(i6)) { - code 40 - } else { - code 39 - } - code 41 - if (uint(i5) >= uint(i6)) { - code 43 - } else { - code 42 - } - code 44 - if (uint(i5) >= uint(i6)) { - code 46 - } else { - code 45 - } - code 47 - if (uint(i5) >= uint(i6)) { - code 49 - } else { - code 48 - } - code 50 - if (uint(i5) >= uint(i6)) { - code 52 - } else { - code 51 - } - code 53 - if (uint(i5) >= uint(i6)) { - code 55 - } else { - code 54 - } - code 56 - if (uint(i5) >= uint(i6)) { - code 58 - } else { - code 57 - } - code 59 - if (uint(i5) >= uint(i6)) { - code 61 - } else { - code 60 - } - code 62 - if (uint(i5) >= uint(i6)) { - code 64 - } else { - code 63 - } - code 65 - if (uint(i5) >= uint(i6)) { - code 67 - } else { - code 66 - } - code 68 - if (uint(i5) >= uint(i6)) { - code 70 - } else { - code 69 - } - code 71 - if (uint(i5) >= uint(i6)) { - code 73 - } else { - code 72 - } - code 74 - if (uint(i5) >= uint(i6)) { - code 76 - } else { - code 75 - } - code 77 - if (uint(i5) >= uint(i6)) { - code 79 - } else { - code 78 - } - code 80 - if (uint(i5) >= uint(i6)) { - code 82 - } else { - code 81 - } - code 83 - if (uint(i5) >= uint(i6)) { - code 85 - } else { - code 84 - } - code 86 - if (uint(i5) >= uint(i6)) { - code 88 - } else { - code 87 - } - code 89 - if (uint(i5) >= uint(i6)) { - code 91 - } else { - code 90 - } - code 92 - if (uint(i5) >= uint(i6)) { - code 94 - } else { - code 93 - } - code 95 - if (uint(i5) >= uint(i6)) { - code 97 - } else { - code 96 - } - code 98 - if (uint(i5) >= uint(i6)) { - code 100 - } else { - code 99 - } - code 101 - if (!(i2 != 0)) { - break L26; +L26: do { + if (!(i2 == 0)) { + code 19 + while(1) { + code 20 + if (uint(i5) >= uint(i6)) { + code 22 + } else { + code 21 + } + code 23 + if (uint(i5) >= uint(i6)) { + code 25 + } else { + code 24 + } + code 26 + if (uint(i5) >= uint(i6)) { + code 28 + } else { + code 27 + } + code 29 + if (uint(i5) >= uint(i6)) { + code 31 + } else { + code 30 + } + code 32 + if (uint(i5) >= uint(i6)) { + code 34 + } else { + code 33 + } + code 35 + if (uint(i5) >= uint(i6)) { + code 37 + } else { + code 36 + } + code 38 + if (uint(i5) >= uint(i6)) { + code 40 + } else { + code 39 + } + code 41 + if (uint(i5) >= uint(i6)) { + code 43 + } else { + code 42 + } + code 44 + if (uint(i5) >= uint(i6)) { + code 46 + } else { + code 45 + } + code 47 + if (uint(i5) >= uint(i6)) { + code 49 + } else { + code 48 + } + code 50 + if (uint(i5) >= uint(i6)) { + code 52 + } else { + code 51 + } + code 53 + if (uint(i5) >= uint(i6)) { + code 55 + } else { + code 54 + } + code 56 + if (uint(i5) >= uint(i6)) { + code 58 + } else { + code 57 + } + code 59 + if (uint(i5) >= uint(i6)) { + code 61 + } else { + code 60 + } + code 62 + if (uint(i5) >= uint(i6)) { + code 64 + } else { + code 63 + } + code 65 + if (uint(i5) >= uint(i6)) { + code 67 + } else { + code 66 + } + code 68 + if (uint(i5) >= uint(i6)) { + code 70 + } else { + code 69 + } + code 71 + if (uint(i5) >= uint(i6)) { + code 73 + } else { + code 72 + } + code 74 + if (uint(i5) >= uint(i6)) { + code 76 + } else { + code 75 + } + code 77 + if (uint(i5) >= uint(i6)) { + code 79 + } else { + code 78 + } + code 80 + if (uint(i5) >= uint(i6)) { + code 82 + } else { + code 81 + } + code 83 + if (uint(i5) >= uint(i6)) { + code 85 + } else { + code 84 + } + code 86 + if (uint(i5) >= uint(i6)) { + code 88 + } else { + code 87 + } + code 89 + if (uint(i5) >= uint(i6)) { + code 91 + } else { + code 90 + } + code 92 + if (uint(i5) >= uint(i6)) { + code 94 + } else { + code 93 + } + code 95 + if (uint(i5) >= uint(i6)) { + code 97 + } else { + code 96 + } + code 98 + if (uint(i5) >= uint(i6)) { + code 100 + } else { + code 99 + } + code 101 + if (!(i2 != 0)) { + break L26; + } } } -} +} while(0); code 102 if (uint(i4) >= uint(i5)) { code 104 @@ -219,136 +221,137 @@ if (uint(i4) >= uint(i5)) { code 103 } code 105 -L143: -if (!(i2 == 0)) { - code 106 - while(1) { - code 107 - if (uint(i5) >= uint(i6)) { - code 109 - } else { - code 108 - } - code 110 - if (uint(i5) >= uint(i6)) { - code 112 - } else { - code 111 - } - code 113 - if (uint(i5) >= uint(i6)) { - code 115 - } else { - code 114 - } - code 116 - if (uint(i5) >= uint(i6)) { - code 118 - } else { - code 117 - } - code 119 - if (uint(i5) >= uint(i6)) { - code 121 - } else { - code 120 - } - code 122 - if (uint(i5) >= uint(i6)) { - code 124 - } else { - code 123 - } - code 125 - if (uint(i5) >= uint(i6)) { - code 127 - } else { - code 126 - } - code 128 - if (uint(i5) >= uint(i6)) { - code 130 - } else { - code 129 - } - code 131 - if (uint(i5) >= uint(i6)) { - code 133 - } else { - code 132 - } - code 134 - if (uint(i5) >= uint(i6)) { - code 136 - } else { - code 135 - } - code 137 - if (uint(i5) >= uint(i6)) { - code 139 - } else { - code 138 - } - code 140 - if (uint(i5) >= uint(i6)) { - code 142 - } else { - code 141 - } - code 143 - if (uint(i5) >= uint(i6)) { - code 145 - } else { - code 144 - } - code 146 - if (uint(i5) >= uint(i6)) { - code 148 - } else { - code 147 - } - code 149 - if (uint(i5) >= uint(i6)) { - code 151 - } else { - code 150 - } - code 152 - if (uint(i5) >= uint(i6)) { - code 154 - } else { - code 153 - } - code 155 - if (uint(i5) >= uint(i6)) { - code 157 - } else { - code 156 - } - code 158 - if (uint(i5) >= uint(i6)) { - code 160 - } else { - code 159 - } - code 161 - if (uint(i5) >= uint(i6)) { - code 163 - } else { - code 162 - } - code 164 - if (uint(i5) >= uint(i6)) { - code 166 - } else { - code 165 - } - code 167 - if (!(i2 != 0)) { - break L143; +L143: do { + if (!(i2 == 0)) { + code 106 + while(1) { + code 107 + if (uint(i5) >= uint(i6)) { + code 109 + } else { + code 108 + } + code 110 + if (uint(i5) >= uint(i6)) { + code 112 + } else { + code 111 + } + code 113 + if (uint(i5) >= uint(i6)) { + code 115 + } else { + code 114 + } + code 116 + if (uint(i5) >= uint(i6)) { + code 118 + } else { + code 117 + } + code 119 + if (uint(i5) >= uint(i6)) { + code 121 + } else { + code 120 + } + code 122 + if (uint(i5) >= uint(i6)) { + code 124 + } else { + code 123 + } + code 125 + if (uint(i5) >= uint(i6)) { + code 127 + } else { + code 126 + } + code 128 + if (uint(i5) >= uint(i6)) { + code 130 + } else { + code 129 + } + code 131 + if (uint(i5) >= uint(i6)) { + code 133 + } else { + code 132 + } + code 134 + if (uint(i5) >= uint(i6)) { + code 136 + } else { + code 135 + } + code 137 + if (uint(i5) >= uint(i6)) { + code 139 + } else { + code 138 + } + code 140 + if (uint(i5) >= uint(i6)) { + code 142 + } else { + code 141 + } + code 143 + if (uint(i5) >= uint(i6)) { + code 145 + } else { + code 144 + } + code 146 + if (uint(i5) >= uint(i6)) { + code 148 + } else { + code 147 + } + code 149 + if (uint(i5) >= uint(i6)) { + code 151 + } else { + code 150 + } + code 152 + if (uint(i5) >= uint(i6)) { + code 154 + } else { + code 153 + } + code 155 + if (uint(i5) >= uint(i6)) { + code 157 + } else { + code 156 + } + code 158 + if (uint(i5) >= uint(i6)) { + code 160 + } else { + code 159 + } + code 161 + if (uint(i5) >= uint(i6)) { + code 163 + } else { + code 162 + } + code 164 + if (uint(i5) >= uint(i6)) { + code 166 + } else { + code 165 + } + code 167 + if (!(i2 != 0)) { + break L143; + } } } -} +} while(0); code 168 if (uint(i4) >= uint(i5)) { code 170 diff --git a/src/runtime.js b/src/runtime.js index e5b86065..dc604a8d 100644 --- a/src/runtime.js +++ b/src/runtime.js @@ -25,7 +25,7 @@ var RuntimeGenerator = { sep = sep || ';'; if (USE_TYPED_ARRAYS === 2) 'STACKTOP = (STACKTOP + STACKTOP|0 % ' + ({{{ QUANTUM_SIZE }}} - (isNumber(size) ? Math.min(size, {{{ QUANTUM_SIZE }}}) : {{{ QUANTUM_SIZE }}})) + ')' + sep; // The stack is always QUANTUM SIZE aligned, so we may not need to force alignment here - var ret = RuntimeGenerator.alloc(size, 'STACK', INIT_STACK, sep, USE_TYPED_ARRAYS != 2 || (isNumber(size) && parseInt(size) % {{{ QUANTUM_SIZE }}} == 0)); + var ret = RuntimeGenerator.alloc(size, 'STACK', false, sep, USE_TYPED_ARRAYS != 2 || (isNumber(size) && parseInt(size) % {{{ QUANTUM_SIZE }}} == 0)); if (ASSERTIONS) { ret += sep + 'assert(STACKTOP|0 < STACK_MAX|0)'; } @@ -45,7 +45,7 @@ var RuntimeGenerator = { if (ASSERTIONS) { ret += '; assert(STACKTOP < STACK_MAX)'; } - if (INIT_STACK) { + if (false) { ret += '; _memset(' + asmCoercion('__stackBase__', 'i32') + ', 0, ' + initial + ')'; } return ret; @@ -242,6 +242,10 @@ var Runtime = { } else if (Runtime.isStructType(field)) { size = Types.types[field].flatSize; alignSize = Types.types[field].alignSize; + } else if (field[0] == 'b') { + // bN, large number field, like a [N x i8] + size = field.substr(1)|0; + alignSize = 1; } else { throw 'Unclear type in struct: ' + field + ', in ' + type.name_ + ' :: ' + dump(Types.types[type.name_]); } @@ -353,7 +357,7 @@ var Runtime = { }, addFunction: function(func, sig) { - assert(sig); + //assert(sig); // TODO: support asm var table = FUNCTION_TABLE; // TODO: support asm var ret = table.length; table.push(func); @@ -361,6 +365,11 @@ var Runtime = { return ret; }, + removeFunction: function(index) { + var table = FUNCTION_TABLE; // TODO: support asm + table[index] = null; + }, + warnOnce: function(text) { if (!Runtime.warnOnce.shown) Runtime.warnOnce.shown = {}; if (!Runtime.warnOnce.shown[text]) { @@ -505,26 +514,19 @@ function getRuntime() { // example, -1 in int32 would be a very large number as unsigned. function unSign(value, bits, ignore, sig) { if (value >= 0) { -#if CHECK_SIGNS - if (!ignore) CorrectionsMonitor.note('UnSign', 1, sig); -#endif return value; } #if CHECK_SIGNS - if (!ignore) CorrectionsMonitor.note('UnSign', 0, sig); + if (!ignore) throw 'UnSign'; #endif return bits <= 32 ? 2*Math.abs(1 << (bits-1)) + value // Need some trickery, since if bits == 32, we are right at the limit of the bits JS uses in bitshifts : Math.pow(2, bits) + value; - // TODO: clean up previous line } // Converts a value we have as unsigned, into a signed value. For // example, 200 in a uint8 would be a negative number. function reSign(value, bits, ignore, sig) { if (value <= 0) { -#if CHECK_SIGNS - if (!ignore) CorrectionsMonitor.note('ReSign', 1, sig); -#endif return value; } var half = bits <= 32 ? Math.abs(1 << (bits-1)) // abs is needed if bits == 32 @@ -536,10 +538,7 @@ function reSign(value, bits, ignore, sig) { // but, in general there is no perfect solution here. With 64-bit ints, we get rounding and errors // TODO: In i64 mode 1, resign the two parts separately and safely #if CHECK_SIGNS - if (!ignore) { - CorrectionsMonitor.note('ReSign', 0, sig); - noted = true; - } + if (!ignore) throw 'ReSign'; #endif value = -2*half + value; // Cannot bitshift half, as it may be at the limit of the bits JS uses in bitshifts } @@ -548,18 +547,9 @@ function reSign(value, bits, ignore, sig) { // without CHECK_SIGNS, we would just do the |0 shortcut, so check that that // would indeed give the exact same result. if (bits === 32 && (value|0) !== value && typeof value !== 'boolean') { - if (!ignore) { - CorrectionsMonitor.note('ReSign', 0, sig); - noted = true; - } + if (!ignore) throw 'ReSign'; } - if (!noted) CorrectionsMonitor.note('ReSign', 1, sig); #endif return value; } -// Just a stub. We don't care about noting compile-time corrections. But they are called. -var CorrectionsMonitor = { - note: function(){} -}; - diff --git a/src/settings.js b/src/settings.js index 0234d0ca..1bfcf92a 100644 --- a/src/settings.js +++ b/src/settings.js @@ -37,7 +37,6 @@ var VERBOSE = 0; // When set to 1, will generate more verbose output during comp var INVOKE_RUN = 1; // Whether we will call run(). Disable if you embed the generated // code in your own, and will call run() yourself at the right time -var INIT_STACK = 0; // Whether to initialize memory on the stack to 0. var INIT_HEAP = 0; // Whether to initialize memory anywhere other than the stack to 0. var TOTAL_STACK = 5*1024*1024; // The total stack size. There is no way to enlarge the stack, so this // value must be large enough for the program's requirements. If @@ -59,6 +58,8 @@ var ALLOW_MEMORY_GROWTH = 0; // If false, we abort with an error if we try to al // Code embetterments var MICRO_OPTS = 1; // Various micro-optimizations, like nativizing variables var RELOOP = 0; // Recreate js native loops from llvm data +var RELOOPER = 'relooper.js'; // Loads the relooper from this path relative to compiler.js + var USE_TYPED_ARRAYS = 2; // Use typed arrays for the heap. See https://github.com/kripken/emscripten/wiki/Code-Generation-Modes/ // 0 means no typed arrays are used. // 1 has two heaps, IHEAP (int32) and FHEAP (double), @@ -92,13 +93,9 @@ var PRECISE_I64_MATH = 1; // If enabled, i64 addition etc. is emulated - which i // that we can't know at compile time that 64-bit math is needed. For example, if you // print 64-bit values with printf, but never add them, we can't know at compile time // and you need to set this to 2. -var PRECISE_I32_MUL = 0; // If enabled, i64 math is done in i32 multiplication. This is necessary if the values - // exceed the JS double-integer limit of ~52 bits. This option can normally be disabled - // because generally i32 multiplication works ok without it, and enabling it has a big - // impact on performance. - // Note that you can hand-optimize your code to avoid the need for this: If you do - // multiplications that actually need 64-bit precision inside 64-bit values, things - // will work properly. (Unless the LLVM optimizer turns them into 32-bit values?) +var PRECISE_I32_MUL = 1; // If enabled, i32 multiplication is done with full precision, which means it is + // correct even if the value exceeds the JS double-integer limit of ~52 bits (otherwise, + // rounding will occur above that range). var CLOSURE_ANNOTATIONS = 0; // If set, the generated code will be annotated for the closure // compiler. This potentially lets closure optimize the code better. @@ -111,10 +108,11 @@ var SKIP_STACK_IN_SMALL = 1; // When enabled, does not push/pop the stack at all // In particular, be careful with the autodebugger! (We do turn // this off automatically in that case, though.) var INLINE_LIBRARY_FUNCS = 1; // Will inline library functions that have __inline defined -var INLINING_LIMIT = 50; // A limit on inlining. If 0, we will inline normally in LLVM and +var INLINING_LIMIT = 0; // A limit on inlining. If 0, we will inline normally in LLVM and // closure. If greater than 0, we will *not* inline in LLVM, and // we will prevent inlining of functions of this size or larger - // in closure. + // in closure. 50 is a reasonable setting if you do not want + // inlining var CATCH_EXIT_CODE = 0; // If set, causes exit() to throw an exception object which is caught // in a try..catch block and results in the exit status being // returned from run(). If zero (the default), the program is just @@ -132,8 +130,23 @@ var SAFE_HEAP = 0; // Check each write to the heap, for example, this will give // that 3 is the option you usually want here. var SAFE_HEAP_LOG = 0; // Log out all SAFE_HEAP operations +var CHECK_HEAP_ALIGN = 0; // Check heap accesses for alignment, but don't do as + // near extensive (or slow) checks as SAFE_HEAP. + +var SAFE_DYNCALLS = 0; // Show stack traces on missing function pointer/virtual method calls + var ASM_HEAP_LOG = 0; // Simple heap logging, like SAFE_HEAP_LOG but cheaper, and in asm.js +var CORRUPTION_CHECK = 0; // When enabled, will emit a buffer area at the beginning and + // end of each allocation on the heap, filled with canary + // values that can be checked later. Corruption is checked for + // at the end of each at each free() (see jsifier to add more, and you + // can add more manual checks by calling CorruptionChecker.checkAll). + // 0 means not enabled, higher values mean the size of the + // buffer areas as a multiple of the allocated area (so + // 1 means 100%, or buffer areas equal to allocated area, + // both before and after). This must be an integer. + var LABEL_DEBUG = 0; // 1: Print out functions as we enter them // 2: Also print out each label as we enter it var LABEL_FUNCTION_FILTERS = []; // Filters for function label debug. @@ -148,11 +161,15 @@ var LIBRARY_DEBUG = 0; // Print out when we enter a library call (library*.js). // Runtime.debug at runtime for logging to cease, and can set it when you // want it back. A simple way to set it in C++ is // emscripten_run_script("Runtime.debug = ...;"); -var GL_DEBUG = 0; // Print out all calls into WebGL. As with LIBRARY_DEBUG, you can set a runtime - // option, in this case GL.debug. var SOCKET_DEBUG = 0; // Log out socket/network data transfer. -var PROFILE_MAIN_LOOP = 0; // Profile the function called in set_main_loop +var GL_DEBUG = 0; // Print out all calls into WebGL. As with LIBRARY_DEBUG, you can set a runtime + // option, in this case GL.debug. +var GL_TESTING = 0; // When enabled, sets preserveDrawingBuffer in the context, to allow tests to work (but adds overhead) +var GL_MAX_TEMP_BUFFER_SIZE = 2097152; // How large GL emulation temp buffers are +var GL_UNSAFE_OPTS = 1; // Enables some potentially-unsafe optimizations in GL emulation code +var FULL_ES2 = 0; // Forces support for all GLES2 features, not just the WebGL-friendly subset. +var FORCE_GL_EMULATION = 0; // Forces inclusion of full GL emulation code. var DISABLE_EXCEPTION_CATCHING = 0; // Disables generating code to actually catch exceptions. If the code you // are compiling does not actually rely on catching exceptions (but the @@ -203,21 +220,10 @@ var FS_LOG = 0; // Log all FS operations. This is especially helpful when you'r // a new project and want to see a list of file system operations happening // so that you can create a virtual file system with all of the required files. -var PGO = 0; // Profile-guided optimization. - // When run with the CHECK_* options, will not fail on errors. Instead, will - // keep a record of which checks succeeded and which failed. On shutdown, will - // print out that information. This is useful for knowing which lines need - // checking enabled and which do not, that is, this is a way to automate the - // generation of line data for CORRECT_*_LINES options. - // All CORRECT_* options default to 1 with PGO builds. - // See https://github.com/kripken/emscripten/wiki/Optimizing-Code for more info - var NAMED_GLOBALS = 0; // If 1, we use global variables for globals. Otherwise // they are referred to by a base plus an offset (called an indexed global), // saving global variables but adding runtime overhead. -var PROFILE = 0; // Enables runtime profiling. See test_profiling for a usage example. - var EXPORT_ALL = 0; // If true, we export all the symbols var EXPORTED_FUNCTIONS = ['_main']; // Functions that are explicitly exported. These functions are kept alive // through LLVM dead code elimination, and also made accessible outside of @@ -325,12 +331,13 @@ var BENCHMARK = 0; // If 1, will just time how long main() takes to execute, and var ASM_JS = 0; // If 1, generate code in asm.js format. XXX This is highly experimental, // and will not work on most codebases yet. It is NOT recommended that you // try this yet. -var USE_MATH_IMUL = 0; // If 1, use Math.imul when useful var EXPLICIT_ZEXT = 0; // If 1, generate an explicit conversion of zext i1 to i32, using ?: var NECESSARY_BLOCKADDRS = []; // List of (function, block) for all block addresses that are taken. +var EMIT_GENERATED_FUNCTIONS = 0; // whether to emit the list of generated functions, needed for external JS optimization passes + // Compiler debugging options var DEBUG_TAGS_SHOWING = []; // Some useful items: diff --git a/src/shell.html b/src/shell.html index 3a0171de..8743d403 100644 --- a/src/shell.html +++ b/src/shell.html @@ -19,7 +19,14 @@ </div> <canvas class="emscripten" id="canvas" oncontextmenu="event.preventDefault()"></canvas> <hr/> - <div class="emscripten"><input type="button" value="fullscreen" onclick="Module.requestFullScreen()"></div> + <div class="emscripten"> + <input type="checkbox" id="resize">Resize canvas + <input type="checkbox" id="pointerLock" checked>Lock/hide mouse pointer + + <input type="button" value="Fullscreen" onclick="Module.requestFullScreen(document.getElementById('pointerLock').checked, + document.getElementById('resize').checked)"> + </div> + <hr/> <textarea class="emscripten" id="output" rows="8"></textarea> <hr> @@ -32,6 +39,7 @@ var element = document.getElementById('output'); element.value = ''; // clear browser cache return function(text) { + text = Array.prototype.slice.call(arguments).join(' '); // These replacements are necessary if you render to raw HTML //text = text.replace(/&/g, "&"); //text = text.replace(/</g, "<"); @@ -42,6 +50,7 @@ }; })(), printErr: function(text) { + text = Array.prototype.slice.call(arguments).join(' '); if (0) { // XXX disabled for safety typeof dump == 'function') { dump(text + '\n'); // fast, straight to the real console } else { diff --git a/system/include/emscripten/emscripten.h b/system/include/emscripten/emscripten.h index 93551f39..61634b0e 100644 --- a/system/include/emscripten/emscripten.h +++ b/system/include/emscripten/emscripten.h @@ -60,7 +60,7 @@ extern void emscripten_async_run_script(const char *script, int millis); * that execution continues normally. Note that in both cases * we do not run global destructors, atexit, etc., since we * know the main loop will still be running, but if we do - * not simulate an infinite loop then the stack will be unwinded. + * not simulate an infinite loop then the stack will be unwound. * That means that if simulate_infinite_loop is false, and * you created an object on the stack, it will be cleaned up * before the main loop will be called the first time. @@ -215,7 +215,7 @@ void emscripten_async_wget_data(const char* url, void *arg, void (*onload)(void* * More feature-complete version of emscripten_async_wget. Note: * this version is experimental. * - * The requestype is 'GET' or 'POST', + * The requesttype is 'GET' or 'POST', * If is post request, param is the post parameter * like key=value&key2=value2. * The param 'arg' is a pointer will be pass to the callback @@ -345,6 +345,30 @@ extern void EMSCRIPTEN_PROFILE_INIT(int max); extern void EMSCRIPTEN_PROFILE_BEGIN(int id); extern void EMSCRIPTEN_PROFILE_END(int id); +/* + * jcache-friendly printf. printf in general will receive a string + * literal, which becomes a global constant, which invalidates all + * jcache entries. emscripten_jcache_printf is parsed before + * clang into something without any string literals, so you can + * add such printouts to your code and only the (chunk containing + * the) function you modify will be invalided and recompiled. + * + * Note in particular that you need to already have a call to this + * function in your code *before* you add one and do an incremental + * build, so that adding an external reference does not invalidate + * everything. + * + * This function assumes the first argument is a string literal + * (otherwise you don't need it), and the other arguments, if any, + * are neither strings nor complex expressions (but just simple + * variables). (You can create a variable to store a complex + * expression on the previous line, if necessary.) + */ +#ifdef __cplusplus +void emscripten_jcache_printf(const char *format, ...); +void emscripten_jcache_printf_(...); /* internal use */ +#endif + #ifdef __cplusplus } #endif diff --git a/system/include/err.h b/system/include/err.h new file mode 100644 index 00000000..a9b92ee6 --- /dev/null +++ b/system/include/err.h @@ -0,0 +1,95 @@ +/* $OpenBSD: err.h,v 1.10 2006/01/06 18:53:04 millert Exp $ */ +/* $NetBSD: err.h,v 1.11 1994/10/26 00:55:52 cgd Exp $ */ + +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * @(#)err.h 8.1 (Berkeley) 6/2/93 + */ + +#ifndef _ERR_H_ +#define _ERR_H_ + +/* + * Don't use va_list in the err/warn prototypes. Va_list is typedef'd in two + * places (<machine/varargs.h> and <machine/stdarg.h>), so if we include one + * of them here we may collide with the utility's includes. It's unreasonable + * for utilities to have to include one of them to include err.h, so we get + * va_list from <machine/_types.h> and use it. + */ +#include <sys/cdefs.h> +//#include <machine/_types.h> +#include <stdarg.h> + +#define __dead __attribute__((__noreturn__)) + +__BEGIN_DECLS + +__dead void err(int, const char *, ...) + __attribute__((__format__ (printf, 2, 3))); +__dead void verr(int, const char *, va_list) + __attribute__((__format__ (printf, 2, 0))); +__dead void errx(int, const char *, ...) + __attribute__((__format__ (printf, 2, 3))); +__dead void verrx(int, const char *, va_list) + __attribute__((__format__ (printf, 2, 0))); +void warn(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +void vwarn(const char *, va_list) + __attribute__((__format__ (printf, 1, 0))); +void warnx(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +void vwarnx(const char *, va_list) + __attribute__((__format__ (printf, 1, 0))); + +/* + * The _* versions are for use in library functions so user-defined + * versions of err*,warn* do not get used. + */ +__dead void _err(int, const char *, ...) + __attribute__((__format__ (printf, 2, 3))); +__dead void _verr(int, const char *, va_list) + __attribute__((__format__ (printf, 2, 0))); +__dead void _errx(int, const char *, ...) + __attribute__((__format__ (printf, 2, 3))); +__dead void _verrx(int, const char *, va_list) + __attribute__((__format__ (printf, 2, 0))); +void _warn(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +void _vwarn(const char *, va_list) + __attribute__((__format__ (printf, 1, 0))); +void _warnx(const char *, ...) + __attribute__((__format__ (printf, 1, 2))); +void _vwarnx(const char *, va_list) + __attribute__((__format__ (printf, 1, 0))); + +#undef __dead + +__END_DECLS + +#endif /* !_ERR_H_ */ diff --git a/system/include/stdbool.h b/system/include/stdbool.h index f970ade8..561eed3f 100644 --- a/system/include/stdbool.h +++ b/system/include/stdbool.h @@ -2,12 +2,13 @@ #ifndef __stdbool_h__ #define __stdbool_h__ +#define __bool_true_false_are_defined 1 + #ifndef __cplusplus #define bool _Bool #define true 1 #define false 0 -#define __bool_true_false_are_defined 1 #endif diff --git a/system/lib/dlmalloc.c b/system/lib/dlmalloc.c index 9ee3709e..7e04a726 100644 --- a/system/lib/dlmalloc.c +++ b/system/lib/dlmalloc.c @@ -5,492 +5,537 @@ /* - This is a version (aka dlmalloc) of malloc/free/realloc written by - Doug Lea and released to the public domain, as explained at - http://creativecommons.org/licenses/publicdomain. Send questions, - comments, complaints, performance data, etc to dl@cs.oswego.edu - -* Version 2.8.4 Wed May 27 09:56:23 2009 Doug Lea (dl at gee) - - Note: There may be an updated version of this malloc obtainable at - ftp://gee.cs.oswego.edu/pub/misc/malloc.c - Check before installing! - -* Quickstart - - This library is all in one file to simplify the most common usage: - ftp it, compile it (-O3), and link it into another program. All of - the compile-time options default to reasonable values for use on - most platforms. You might later want to step through various - compile-time and dynamic tuning options. - - For convenience, an include file for code using this malloc is at: - ftp://gee.cs.oswego.edu/pub/misc/malloc-2.8.4.h - You don't really need this .h file unless you call functions not - defined in your system include files. The .h file contains only the - excerpts from this file needed for using this malloc on ANSI C/C++ - systems, so long as you haven't changed compile-time options about - naming and tuning parameters. If you do, then you can create your - own malloc.h that does include all settings by cutting at the point - indicated below. Note that you may already by default be using a C - library containing a malloc that is based on some version of this - malloc (for example in linux). You might still want to use the one - in this file to customize settings or to avoid overheads associated - with library versions. - -* Vital statistics: - - Supported pointer/size_t representation: 4 or 8 bytes - size_t MUST be an unsigned type of the same width as - pointers. (If you are using an ancient system that declares - size_t as a signed type, or need it to be a different width - than pointers, you can use a previous release of this malloc - (e.g. 2.7.2) supporting these.) - - Alignment: 8 bytes (default) - This suffices for nearly all current machines and C compilers. - However, you can define MALLOC_ALIGNMENT to be wider than this - if necessary (up to 128bytes), at the expense of using more space. - - Minimum overhead per allocated chunk: 4 or 8 bytes (if 4byte sizes) - 8 or 16 bytes (if 8byte sizes) - Each malloced chunk has a hidden word of overhead holding size - and status information, and additional cross-check word - if FOOTERS is defined. - - Minimum allocated size: 4-byte ptrs: 16 bytes (including overhead) - 8-byte ptrs: 32 bytes (including overhead) - - Even a request for zero bytes (i.e., malloc(0)) returns a - pointer to something of the minimum allocatable size. - The maximum overhead wastage (i.e., number of extra bytes - allocated than were requested in malloc) is less than or equal - to the minimum size, except for requests >= mmap_threshold that - are serviced via mmap(), where the worst case wastage is about - 32 bytes plus the remainder from a system page (the minimal - mmap unit); typically 4096 or 8192 bytes. - - Security: static-safe; optionally more or less - The "security" of malloc refers to the ability of malicious - code to accentuate the effects of errors (for example, freeing - space that is not currently malloc'ed or overwriting past the - ends of chunks) in code that calls malloc. This malloc - guarantees not to modify any memory locations below the base of - heap, i.e., static variables, even in the presence of usage - errors. The routines additionally detect most improper frees - and reallocs. All this holds as long as the static bookkeeping - for malloc itself is not corrupted by some other means. This - is only one aspect of security -- these checks do not, and - cannot, detect all possible programming errors. - - If FOOTERS is defined nonzero, then each allocated chunk - carries an additional check word to verify that it was malloced - from its space. These check words are the same within each - execution of a program using malloc, but differ across - executions, so externally crafted fake chunks cannot be - freed. This improves security by rejecting frees/reallocs that - could corrupt heap memory, in addition to the checks preventing - writes to statics that are always on. This may further improve - security at the expense of time and space overhead. (Note that - FOOTERS may also be worth using with MSPACES.) - - By default detected errors cause the program to abort (calling - "abort()"). You can override this to instead proceed past - errors by defining PROCEED_ON_ERROR. In this case, a bad free - has no effect, and a malloc that encounters a bad address - caused by user overwrites will ignore the bad address by - dropping pointers and indices to all known memory. This may - be appropriate for programs that should continue if at all - possible in the face of programming errors, although they may - run out of memory because dropped memory is never reclaimed. - - If you don't like either of these options, you can define - CORRUPTION_ERROR_ACTION and USAGE_ERROR_ACTION to do anything - else. And if if you are sure that your program using malloc has - no errors or vulnerabilities, you can define INSECURE to 1, - which might (or might not) provide a small performance improvement. - - Thread-safety: NOT thread-safe unless USE_LOCKS defined - When USE_LOCKS is defined, each public call to malloc, free, - etc is surrounded with either a pthread mutex or a win32 - spinlock (depending on WIN32). This is not especially fast, and - can be a major bottleneck. It is designed only to provide - minimal protection in concurrent environments, and to provide a - basis for extensions. If you are using malloc in a concurrent - program, consider instead using nedmalloc - (http://www.nedprod.com/programs/portable/nedmalloc/) or - ptmalloc (See http://www.malloc.de), which are derived - from versions of this malloc. - - System requirements: Any combination of MORECORE and/or MMAP/MUNMAP - This malloc can use unix sbrk or any emulation (invoked using - the CALL_MORECORE macro) and/or mmap/munmap or any emulation - (invoked using CALL_MMAP/CALL_MUNMAP) to get and release system - memory. On most unix systems, it tends to work best if both - MORECORE and MMAP are enabled. On Win32, it uses emulations - based on VirtualAlloc. It also uses common C library functions - like memset. - - Compliance: I believe it is compliant with the Single Unix Specification - (See http://www.unix.org). Also SVID/XPG, ANSI C, and probably - others as well. - -* Overview of algorithms - - This is not the fastest, most space-conserving, most portable, or - most tunable malloc ever written. However it is among the fastest - while also being among the most space-conserving, portable and - tunable. Consistent balance across these factors results in a good - general-purpose allocator for malloc-intensive programs. - - In most ways, this malloc is a best-fit allocator. Generally, it - chooses the best-fitting existing chunk for a request, with ties - broken in approximately least-recently-used order. (This strategy - normally maintains low fragmentation.) However, for requests less - than 256bytes, it deviates from best-fit when there is not an - exactly fitting available chunk by preferring to use space adjacent - to that used for the previous small request, as well as by breaking - ties in approximately most-recently-used order. (These enhance - locality of series of small allocations.) And for very large requests - (>= 256Kb by default), it relies on system memory mapping - facilities, if supported. (This helps avoid carrying around and - possibly fragmenting memory used only for large chunks.) - - All operations (except malloc_stats and mallinfo) have execution - times that are bounded by a constant factor of the number of bits in - a size_t, not counting any clearing in calloc or copying in realloc, - or actions surrounding MORECORE and MMAP that have times - proportional to the number of non-contiguous regions returned by - system allocation routines, which is often just 1. In real-time - applications, you can optionally suppress segment traversals using - NO_SEGMENT_TRAVERSAL, which assures bounded execution even when - system allocators return non-contiguous spaces, at the typical - expense of carrying around more memory and increased fragmentation. - - The implementation is not very modular and seriously overuses - macros. Perhaps someday all C compilers will do as good a job - inlining modular code as can now be done by brute-force expansion, - but now, enough of them seem not to. - - Some compilers issue a lot of warnings about code that is - dead/unreachable only on some platforms, and also about intentional - uses of negation on unsigned types. All known cases of each can be - ignored. - - For a longer but out of date high-level description, see - http://gee.cs.oswego.edu/dl/html/malloc.html - -* MSPACES - If MSPACES is defined, then in addition to malloc, free, etc., - this file also defines mspace_malloc, mspace_free, etc. These - are versions of malloc routines that take an "mspace" argument - obtained using create_mspace, to control all internal bookkeeping. - If ONLY_MSPACES is defined, only these versions are compiled. - So if you would like to use this allocator for only some allocations, - and your system malloc for others, you can compile with - ONLY_MSPACES and then do something like... - static mspace mymspace = create_mspace(0,0); // for example - #define mymalloc(bytes) mspace_malloc(mymspace, bytes) - - (Note: If you only need one instance of an mspace, you can instead - use "USE_DL_PREFIX" to relabel the global malloc.) - - You can similarly create thread-local allocators by storing - mspaces as thread-locals. For example: - static __thread mspace tlms = 0; - void* tlmalloc(size_t bytes) { - if (tlms == 0) tlms = create_mspace(0, 0); - return mspace_malloc(tlms, bytes); - } - void tlfree(void* mem) { mspace_free(tlms, mem); } - - Unless FOOTERS is defined, each mspace is completely independent. - You cannot allocate from one and free to another (although - conformance is only weakly checked, so usage errors are not always - caught). If FOOTERS is defined, then each chunk carries around a tag - indicating its originating mspace, and frees are directed to their - originating spaces. - + This is a version (aka dlmalloc) of malloc/free/realloc written by + Doug Lea and released to the public domain, as explained at + http://creativecommons.org/publicdomain/zero/1.0/ Send questions, + comments, complaints, performance data, etc to dl@cs.oswego.edu + + * Version 2.8.6 Wed Aug 29 06:57:58 2012 Doug Lea + Note: There may be an updated version of this malloc obtainable at + ftp://gee.cs.oswego.edu/pub/misc/malloc.c + Check before installing! + + * Quickstart + + This library is all in one file to simplify the most common usage: + ftp it, compile it (-O3), and link it into another program. All of + the compile-time options default to reasonable values for use on + most platforms. You might later want to step through various + compile-time and dynamic tuning options. + + For convenience, an include file for code using this malloc is at: + ftp://gee.cs.oswego.edu/pub/misc/malloc-2.8.6.h + You don't really need this .h file unless you call functions not + defined in your system include files. The .h file contains only the + excerpts from this file needed for using this malloc on ANSI C/C++ + systems, so long as you haven't changed compile-time options about + naming and tuning parameters. If you do, then you can create your + own malloc.h that does include all settings by cutting at the point + indicated below. Note that you may already by default be using a C + library containing a malloc that is based on some version of this + malloc (for example in linux). You might still want to use the one + in this file to customize settings or to avoid overheads associated + with library versions. + + * Vital statistics: + + Supported pointer/size_t representation: 4 or 8 bytes + size_t MUST be an unsigned type of the same width as + pointers. (If you are using an ancient system that declares + size_t as a signed type, or need it to be a different width + than pointers, you can use a previous release of this malloc + (e.g. 2.7.2) supporting these.) + + Alignment: 8 bytes (minimum) + This suffices for nearly all current machines and C compilers. + However, you can define MALLOC_ALIGNMENT to be wider than this + if necessary (up to 128bytes), at the expense of using more space. + + Minimum overhead per allocated chunk: 4 or 8 bytes (if 4byte sizes) + 8 or 16 bytes (if 8byte sizes) + Each malloced chunk has a hidden word of overhead holding size + and status information, and additional cross-check word + if FOOTERS is defined. + + Minimum allocated size: 4-byte ptrs: 16 bytes (including overhead) + 8-byte ptrs: 32 bytes (including overhead) + + Even a request for zero bytes (i.e., malloc(0)) returns a + pointer to something of the minimum allocatable size. + The maximum overhead wastage (i.e., number of extra bytes + allocated than were requested in malloc) is less than or equal + to the minimum size, except for requests >= mmap_threshold that + are serviced via mmap(), where the worst case wastage is about + 32 bytes plus the remainder from a system page (the minimal + mmap unit); typically 4096 or 8192 bytes. + + Security: static-safe; optionally more or less + The "security" of malloc refers to the ability of malicious + code to accentuate the effects of errors (for example, freeing + space that is not currently malloc'ed or overwriting past the + ends of chunks) in code that calls malloc. This malloc + guarantees not to modify any memory locations below the base of + heap, i.e., static variables, even in the presence of usage + errors. The routines additionally detect most improper frees + and reallocs. All this holds as long as the static bookkeeping + for malloc itself is not corrupted by some other means. This + is only one aspect of security -- these checks do not, and + cannot, detect all possible programming errors. + + If FOOTERS is defined nonzero, then each allocated chunk + carries an additional check word to verify that it was malloced + from its space. These check words are the same within each + execution of a program using malloc, but differ across + executions, so externally crafted fake chunks cannot be + freed. This improves security by rejecting frees/reallocs that + could corrupt heap memory, in addition to the checks preventing + writes to statics that are always on. This may further improve + security at the expense of time and space overhead. (Note that + FOOTERS may also be worth using with MSPACES.) + + By default detected errors cause the program to abort (calling + "abort()"). You can override this to instead proceed past + errors by defining PROCEED_ON_ERROR. In this case, a bad free + has no effect, and a malloc that encounters a bad address + caused by user overwrites will ignore the bad address by + dropping pointers and indices to all known memory. This may + be appropriate for programs that should continue if at all + possible in the face of programming errors, although they may + run out of memory because dropped memory is never reclaimed. + + If you don't like either of these options, you can define + CORRUPTION_ERROR_ACTION and USAGE_ERROR_ACTION to do anything + else. And if if you are sure that your program using malloc has + no errors or vulnerabilities, you can define INSECURE to 1, + which might (or might not) provide a small performance improvement. + + It is also possible to limit the maximum total allocatable + space, using malloc_set_footprint_limit. This is not + designed as a security feature in itself (calls to set limits + are not screened or privileged), but may be useful as one + aspect of a secure implementation. + + Thread-safety: NOT thread-safe unless USE_LOCKS defined non-zero + When USE_LOCKS is defined, each public call to malloc, free, + etc is surrounded with a lock. By default, this uses a plain + pthread mutex, win32 critical section, or a spin-lock if if + available for the platform and not disabled by setting + USE_SPIN_LOCKS=0. However, if USE_RECURSIVE_LOCKS is defined, + recursive versions are used instead (which are not required for + base functionality but may be needed in layered extensions). + Using a global lock is not especially fast, and can be a major + bottleneck. It is designed only to provide minimal protection + in concurrent environments, and to provide a basis for + extensions. If you are using malloc in a concurrent program, + consider instead using nedmalloc + (http://www.nedprod.com/programs/portable/nedmalloc/) or + ptmalloc (See http://www.malloc.de), which are derived from + versions of this malloc. + + System requirements: Any combination of MORECORE and/or MMAP/MUNMAP + This malloc can use unix sbrk or any emulation (invoked using + the CALL_MORECORE macro) and/or mmap/munmap or any emulation + (invoked using CALL_MMAP/CALL_MUNMAP) to get and release system + memory. On most unix systems, it tends to work best if both + MORECORE and MMAP are enabled. On Win32, it uses emulations + based on VirtualAlloc. It also uses common C library functions + like memset. + + Compliance: I believe it is compliant with the Single Unix Specification + (See http://www.unix.org). Also SVID/XPG, ANSI C, and probably + others as well. + + * Overview of algorithms + + This is not the fastest, most space-conserving, most portable, or + most tunable malloc ever written. However it is among the fastest + while also being among the most space-conserving, portable and + tunable. Consistent balance across these factors results in a good + general-purpose allocator for malloc-intensive programs. + + In most ways, this malloc is a best-fit allocator. Generally, it + chooses the best-fitting existing chunk for a request, with ties + broken in approximately least-recently-used order. (This strategy + normally maintains low fragmentation.) However, for requests less + than 256bytes, it deviates from best-fit when there is not an + exactly fitting available chunk by preferring to use space adjacent + to that used for the previous small request, as well as by breaking + ties in approximately most-recently-used order. (These enhance + locality of series of small allocations.) And for very large requests + (>= 256Kb by default), it relies on system memory mapping + facilities, if supported. (This helps avoid carrying around and + possibly fragmenting memory used only for large chunks.) + + All operations (except malloc_stats and mallinfo) have execution + times that are bounded by a constant factor of the number of bits in + a size_t, not counting any clearing in calloc or copying in realloc, + or actions surrounding MORECORE and MMAP that have times + proportional to the number of non-contiguous regions returned by + system allocation routines, which is often just 1. In real-time + applications, you can optionally suppress segment traversals using + NO_SEGMENT_TRAVERSAL, which assures bounded execution even when + system allocators return non-contiguous spaces, at the typical + expense of carrying around more memory and increased fragmentation. + + The implementation is not very modular and seriously overuses + macros. Perhaps someday all C compilers will do as good a job + inlining modular code as can now be done by brute-force expansion, + but now, enough of them seem not to. + + Some compilers issue a lot of warnings about code that is + dead/unreachable only on some platforms, and also about intentional + uses of negation on unsigned types. All known cases of each can be + ignored. + + For a longer but out of date high-level description, see + http://gee.cs.oswego.edu/dl/html/malloc.html + + * MSPACES + If MSPACES is defined, then in addition to malloc, free, etc., + this file also defines mspace_malloc, mspace_free, etc. These + are versions of malloc routines that take an "mspace" argument + obtained using create_mspace, to control all internal bookkeeping. + If ONLY_MSPACES is defined, only these versions are compiled. + So if you would like to use this allocator for only some allocations, + and your system malloc for others, you can compile with + ONLY_MSPACES and then do something like... + static mspace mymspace = create_mspace(0,0); // for example + #define mymalloc(bytes) mspace_malloc(mymspace, bytes) + + (Note: If you only need one instance of an mspace, you can instead + use "USE_DL_PREFIX" to relabel the global malloc.) + + You can similarly create thread-local allocators by storing + mspaces as thread-locals. For example: + static __thread mspace tlms = 0; + void* tlmalloc(size_t bytes) { + if (tlms == 0) tlms = create_mspace(0, 0); + return mspace_malloc(tlms, bytes); + } + void tlfree(void* mem) { mspace_free(tlms, mem); } + + Unless FOOTERS is defined, each mspace is completely independent. + You cannot allocate from one and free to another (although + conformance is only weakly checked, so usage errors are not always + caught). If FOOTERS is defined, then each chunk carries around a tag + indicating its originating mspace, and frees are directed to their + originating spaces. Normally, this requires use of locks. + ------------------------- Compile-time options --------------------------- - -Be careful in setting #define values for numerical constants of type -size_t. On some systems, literal values are not automatically extended -to size_t precision unless they are explicitly casted. You can also -use the symbolic values MAX_SIZE_T, SIZE_T_ONE, etc below. - -WIN32 default: defined if _WIN32 defined - Defining WIN32 sets up defaults for MS environment and compilers. - Otherwise defaults are for unix. Beware that there seem to be some - cases where this malloc might not be a pure drop-in replacement for - Win32 malloc: Random-looking failures from Win32 GDI API's (eg; - SetDIBits()) may be due to bugs in some video driver implementations - when pixel buffers are malloc()ed, and the region spans more than - one VirtualAlloc()ed region. Because dlmalloc uses a small (64Kb) - default granularity, pixel buffers may straddle virtual allocation - regions more often than when using the Microsoft allocator. You can - avoid this by using VirtualAlloc() and VirtualFree() for all pixel - buffers rather than using malloc(). If this is not possible, - recompile this malloc with a larger DEFAULT_GRANULARITY. - -MALLOC_ALIGNMENT default: (size_t)8 - Controls the minimum alignment for malloc'ed chunks. It must be a - power of two and at least 8, even on machines for which smaller - alignments would suffice. It may be defined as larger than this - though. Note however that code and data structures are optimized for - the case of 8-byte alignment. - -MSPACES default: 0 (false) - If true, compile in support for independent allocation spaces. - This is only supported if HAVE_MMAP is true. - -ONLY_MSPACES default: 0 (false) - If true, only compile in mspace versions, not regular versions. - -USE_LOCKS default: 0 (false) - Causes each call to each public routine to be surrounded with - pthread or WIN32 mutex lock/unlock. (If set true, this can be - overridden on a per-mspace basis for mspace versions.) If set to a - non-zero value other than 1, locks are used, but their - implementation is left out, so lock functions must be supplied manually, - as described below. - -USE_SPIN_LOCKS default: 1 iff USE_LOCKS and on x86 using gcc or MSC - If true, uses custom spin locks for locking. This is currently - supported only for x86 platforms using gcc or recent MS compilers. - Otherwise, posix locks or win32 critical sections are used. - -FOOTERS default: 0 - If true, provide extra checking and dispatching by placing - information in the footers of allocated chunks. This adds - space and time overhead. - -INSECURE default: 0 - If true, omit checks for usage errors and heap space overwrites. - -USE_DL_PREFIX default: NOT defined - Causes compiler to prefix all public routines with the string 'dl'. - This can be useful when you only want to use this malloc in one part - of a program, using your regular system malloc elsewhere. - -ABORT default: defined as abort() - Defines how to abort on failed checks. On most systems, a failed - check cannot die with an "assert" or even print an informative - message, because the underlying print routines in turn call malloc, - which will fail again. Generally, the best policy is to simply call - abort(). It's not very useful to do more than this because many - errors due to overwriting will show up as address faults (null, odd - addresses etc) rather than malloc-triggered checks, so will also - abort. Also, most compilers know that abort() does not return, so - can better optimize code conditionally calling it. - -PROCEED_ON_ERROR default: defined as 0 (false) - Controls whether detected bad addresses cause them to bypassed - rather than aborting. If set, detected bad arguments to free and - realloc are ignored. And all bookkeeping information is zeroed out - upon a detected overwrite of freed heap space, thus losing the - ability to ever return it from malloc again, but enabling the - application to proceed. If PROCEED_ON_ERROR is defined, the - static variable malloc_corruption_error_count is compiled in - and can be examined to see if errors have occurred. This option - generates slower code than the default abort policy. - -DEBUG default: NOT defined - The DEBUG setting is mainly intended for people trying to modify - this code or diagnose problems when porting to new platforms. - However, it may also be able to better isolate user errors than just - using runtime checks. The assertions in the check routines spell - out in more detail the assumptions and invariants underlying the - algorithms. The checking is fairly extensive, and will slow down - execution noticeably. Calling malloc_stats or mallinfo with DEBUG - set will attempt to check every non-mmapped allocated and free chunk - in the course of computing the summaries. - -ABORT_ON_ASSERT_FAILURE default: defined as 1 (true) - Debugging assertion failures can be nearly impossible if your - version of the assert macro causes malloc to be called, which will - lead to a cascade of further failures, blowing the runtime stack. - ABORT_ON_ASSERT_FAILURE cause assertions failures to call abort(), - which will usually make debugging easier. - -MALLOC_FAILURE_ACTION default: sets errno to ENOMEM, or no-op on win32 - The action to take before "return 0" when malloc fails to be able to - return memory because there is none available. - -HAVE_MORECORE default: 1 (true) unless win32 or ONLY_MSPACES - True if this system supports sbrk or an emulation of it. - -MORECORE default: sbrk - The name of the sbrk-style system routine to call to obtain more - memory. See below for guidance on writing custom MORECORE - functions. The type of the argument to sbrk/MORECORE varies across - systems. It cannot be size_t, because it supports negative - arguments, so it is normally the signed type of the same width as - size_t (sometimes declared as "intptr_t"). It doesn't much matter - though. Internally, we only call it with arguments less than half - the max value of a size_t, which should work across all reasonable - possibilities, although sometimes generating compiler warnings. - -MORECORE_CONTIGUOUS default: 1 (true) if HAVE_MORECORE - If true, take advantage of fact that consecutive calls to MORECORE - with positive arguments always return contiguous increasing - addresses. This is true of unix sbrk. It does not hurt too much to - set it true anyway, since malloc copes with non-contiguities. - Setting it false when definitely non-contiguous saves time - and possibly wasted space it would take to discover this though. - -MORECORE_CANNOT_TRIM default: NOT defined - True if MORECORE cannot release space back to the system when given - negative arguments. This is generally necessary only if you are - using a hand-crafted MORECORE function that cannot handle negative - arguments. - -NO_SEGMENT_TRAVERSAL default: 0 - If non-zero, suppresses traversals of memory segments - returned by either MORECORE or CALL_MMAP. This disables - merging of segments that are contiguous, and selectively - releasing them to the OS if unused, but bounds execution times. - -HAVE_MMAP default: 1 (true) - True if this system supports mmap or an emulation of it. If so, and - HAVE_MORECORE is not true, MMAP is used for all system - allocation. If set and HAVE_MORECORE is true as well, MMAP is - primarily used to directly allocate very large blocks. It is also - used as a backup strategy in cases where MORECORE fails to provide - space from system. Note: A single call to MUNMAP is assumed to be - able to unmap memory that may have be allocated using multiple calls - to MMAP, so long as they are adjacent. - -HAVE_MREMAP default: 1 on linux, else 0 - If true realloc() uses mremap() to re-allocate large blocks and - extend or shrink allocation spaces. - -MMAP_CLEARS default: 1 except on WINCE. - True if mmap clears memory so calloc doesn't need to. This is true - for standard unix mmap using /dev/zero and on WIN32 except for WINCE. - -USE_BUILTIN_FFS default: 0 (i.e., not used) - Causes malloc to use the builtin ffs() function to compute indices. - Some compilers may recognize and intrinsify ffs to be faster than the - supplied C version. Also, the case of x86 using gcc is special-cased - to an asm instruction, so is already as fast as it can be, and so - this setting has no effect. Similarly for Win32 under recent MS compilers. - (On most x86s, the asm version is only slightly faster than the C version.) - -malloc_getpagesize default: derive from system includes, or 4096. - The system page size. To the extent possible, this malloc manages - memory from the system in page-size units. This may be (and - usually is) a function rather than a constant. This is ignored - if WIN32, where page size is determined using getSystemInfo during - initialization. - -USE_DEV_RANDOM default: 0 (i.e., not used) - Causes malloc to use /dev/random to initialize secure magic seed for - stamping footers. Otherwise, the current time is used. - -NO_MALLINFO default: 0 - If defined, don't compile "mallinfo". This can be a simple way - of dealing with mismatches between system declarations and - those in this file. - -MALLINFO_FIELD_TYPE default: size_t - The type of the fields in the mallinfo struct. This was originally - defined as "int" in SVID etc, but is more usefully defined as - size_t. The value is used only if HAVE_USR_INCLUDE_MALLOC_H is not set - -REALLOC_ZERO_BYTES_FREES default: not defined - This should be set if a call to realloc with zero bytes should - be the same as a call to free. Some people think it should. Otherwise, - since this malloc returns a unique pointer for malloc(0), so does - realloc(p, 0). - -LACKS_UNISTD_H, LACKS_FCNTL_H, LACKS_SYS_PARAM_H, LACKS_SYS_MMAN_H -LACKS_STRINGS_H, LACKS_STRING_H, LACKS_SYS_TYPES_H, LACKS_ERRNO_H -LACKS_STDLIB_H default: NOT defined unless on WIN32 - Define these if your system does not have these header files. - You might need to manually insert some of the declarations they provide. - -DEFAULT_GRANULARITY default: page size if MORECORE_CONTIGUOUS, - system_info.dwAllocationGranularity in WIN32, - otherwise 64K. - Also settable using mallopt(M_GRANULARITY, x) - The unit for allocating and deallocating memory from the system. On - most systems with contiguous MORECORE, there is no reason to - make this more than a page. However, systems with MMAP tend to - either require or encourage larger granularities. You can increase - this value to prevent system allocation functions to be called so - often, especially if they are slow. The value must be at least one - page and must be a power of two. Setting to 0 causes initialization - to either page size or win32 region size. (Note: In previous - versions of malloc, the equivalent of this option was called - "TOP_PAD") - -DEFAULT_TRIM_THRESHOLD default: 2MB - Also settable using mallopt(M_TRIM_THRESHOLD, x) - The maximum amount of unused top-most memory to keep before - releasing via malloc_trim in free(). Automatic trimming is mainly - useful in long-lived programs using contiguous MORECORE. Because - trimming via sbrk can be slow on some systems, and can sometimes be - wasteful (in cases where programs immediately afterward allocate - more large chunks) the value should be high enough so that your - overall system performance would improve by releasing this much - memory. As a rough guide, you might set to a value close to the - average size of a process (program) running on your system. - Releasing this much memory would allow such a process to run in - memory. Generally, it is worth tuning trim thresholds when a - program undergoes phases where several large chunks are allocated - and released in ways that can reuse each other's storage, perhaps - mixed with phases where there are no such chunks at all. The trim - value must be greater than page size to have any useful effect. To - disable trimming completely, you can set to MAX_SIZE_T. Note that the trick - some people use of mallocing a huge space and then freeing it at - program startup, in an attempt to reserve system memory, doesn't - have the intended effect under automatic trimming, since that memory - will immediately be returned to the system. - -DEFAULT_MMAP_THRESHOLD default: 256K - Also settable using mallopt(M_MMAP_THRESHOLD, x) - The request size threshold for using MMAP to directly service a - request. Requests of at least this size that cannot be allocated - using already-existing space will be serviced via mmap. (If enough - normal freed space already exists it is used instead.) Using mmap - segregates relatively large chunks of memory so that they can be - individually obtained and released from the host system. A request - serviced through mmap is never reused by any other request (at least - not directly; the system may just so happen to remap successive - requests to the same locations). Segregating space in this way has - the benefits that: Mmapped space can always be individually released - back to the system, which helps keep the system level memory demands - of a long-lived program low. Also, mapped memory doesn't become - `locked' between other chunks, as can happen with normally allocated - chunks, which means that even trimming via malloc_trim would not - release them. However, it has the disadvantage that the space - cannot be reclaimed, consolidated, and then used to service later - requests, as happens with normal chunks. The advantages of mmap - nearly always outweigh disadvantages for "large" chunks, but the - value of "large" may vary across systems. The default is an - empirically derived value that works well in most systems. You can - disable mmap by setting to MAX_SIZE_T. - -MAX_RELEASE_CHECK_RATE default: 4095 unless not HAVE_MMAP - The number of consolidated frees between checks to release - unused segments when freeing. When using non-contiguous segments, - especially with multiple mspaces, checking only for topmost space - doesn't always suffice to trigger trimming. To compensate for this, - free() will, with a period of MAX_RELEASE_CHECK_RATE (or the - current number of segments, if greater) try to release unused - segments to the OS when freeing chunks that result in - consolidation. The best value for this parameter is a compromise - between slowing down frees with relatively costly checks that - rarely trigger versus holding on to unused memory. To effectively - disable, set to MAX_SIZE_T. This may lead to a very slight speed - improvement at the expense of carrying around more memory. -*/ + + Be careful in setting #define values for numerical constants of type + size_t. On some systems, literal values are not automatically extended + to size_t precision unless they are explicitly casted. You can also + use the symbolic values MAX_SIZE_T, SIZE_T_ONE, etc below. + + WIN32 default: defined if _WIN32 defined + Defining WIN32 sets up defaults for MS environment and compilers. + Otherwise defaults are for unix. Beware that there seem to be some + cases where this malloc might not be a pure drop-in replacement for + Win32 malloc: Random-looking failures from Win32 GDI API's (eg; + SetDIBits()) may be due to bugs in some video driver implementations + when pixel buffers are malloc()ed, and the region spans more than + one VirtualAlloc()ed region. Because dlmalloc uses a small (64Kb) + default granularity, pixel buffers may straddle virtual allocation + regions more often than when using the Microsoft allocator. You can + avoid this by using VirtualAlloc() and VirtualFree() for all pixel + buffers rather than using malloc(). If this is not possible, + recompile this malloc with a larger DEFAULT_GRANULARITY. Note: + in cases where MSC and gcc (cygwin) are known to differ on WIN32, + conditions use _MSC_VER to distinguish them. + + DLMALLOC_EXPORT default: extern + Defines how public APIs are declared. If you want to export via a + Windows DLL, you might define this as + #define DLMALLOC_EXPORT extern __declspec(dllexport) + If you want a POSIX ELF shared object, you might use + #define DLMALLOC_EXPORT extern __attribute__((visibility("default"))) + + MALLOC_ALIGNMENT default: (size_t)(2 * sizeof(void *)) + Controls the minimum alignment for malloc'ed chunks. It must be a + power of two and at least 8, even on machines for which smaller + alignments would suffice. It may be defined as larger than this + though. Note however that code and data structures are optimized for + the case of 8-byte alignment. + + MSPACES default: 0 (false) + If true, compile in support for independent allocation spaces. + This is only supported if HAVE_MMAP is true. + + ONLY_MSPACES default: 0 (false) + If true, only compile in mspace versions, not regular versions. + + USE_LOCKS default: 0 (false) + Causes each call to each public routine to be surrounded with + pthread or WIN32 mutex lock/unlock. (If set true, this can be + overridden on a per-mspace basis for mspace versions.) If set to a + non-zero value other than 1, locks are used, but their + implementation is left out, so lock functions must be supplied manually, + as described below. + + USE_SPIN_LOCKS default: 1 iff USE_LOCKS and spin locks available + If true, uses custom spin locks for locking. This is currently + supported only gcc >= 4.1, older gccs on x86 platforms, and recent + MS compilers. Otherwise, posix locks or win32 critical sections are + used. + + USE_RECURSIVE_LOCKS default: not defined + If defined nonzero, uses recursive (aka reentrant) locks, otherwise + uses plain mutexes. This is not required for malloc proper, but may + be needed for layered allocators such as nedmalloc. + + LOCK_AT_FORK default: not defined + If defined nonzero, performs pthread_atfork upon initialization + to initialize child lock while holding parent lock. The implementation + assumes that pthread locks (not custom locks) are being used. In other + cases, you may need to customize the implementation. + + FOOTERS default: 0 + If true, provide extra checking and dispatching by placing + information in the footers of allocated chunks. This adds + space and time overhead. + + INSECURE default: 0 + If true, omit checks for usage errors and heap space overwrites. + + USE_DL_PREFIX default: NOT defined + Causes compiler to prefix all public routines with the string 'dl'. + This can be useful when you only want to use this malloc in one part + of a program, using your regular system malloc elsewhere. + + MALLOC_INSPECT_ALL default: NOT defined + If defined, compiles malloc_inspect_all and mspace_inspect_all, that + perform traversal of all heap space. Unless access to these + functions is otherwise restricted, you probably do not want to + include them in secure implementations. + + ABORT default: defined as abort() + Defines how to abort on failed checks. On most systems, a failed + check cannot die with an "assert" or even print an informative + message, because the underlying print routines in turn call malloc, + which will fail again. Generally, the best policy is to simply call + abort(). It's not very useful to do more than this because many + errors due to overwriting will show up as address faults (null, odd + addresses etc) rather than malloc-triggered checks, so will also + abort. Also, most compilers know that abort() does not return, so + can better optimize code conditionally calling it. + + PROCEED_ON_ERROR default: defined as 0 (false) + Controls whether detected bad addresses cause them to bypassed + rather than aborting. If set, detected bad arguments to free and + realloc are ignored. And all bookkeeping information is zeroed out + upon a detected overwrite of freed heap space, thus losing the + ability to ever return it from malloc again, but enabling the + application to proceed. If PROCEED_ON_ERROR is defined, the + static variable malloc_corruption_error_count is compiled in + and can be examined to see if errors have occurred. This option + generates slower code than the default abort policy. + + DEBUG default: NOT defined + The DEBUG setting is mainly intended for people trying to modify + this code or diagnose problems when porting to new platforms. + However, it may also be able to better isolate user errors than just + using runtime checks. The assertions in the check routines spell + out in more detail the assumptions and invariants underlying the + algorithms. The checking is fairly extensive, and will slow down + execution noticeably. Calling malloc_stats or mallinfo with DEBUG + set will attempt to check every non-mmapped allocated and free chunk + in the course of computing the summaries. + + ABORT_ON_ASSERT_FAILURE default: defined as 1 (true) + Debugging assertion failures can be nearly impossible if your + version of the assert macro causes malloc to be called, which will + lead to a cascade of further failures, blowing the runtime stack. + ABORT_ON_ASSERT_FAILURE cause assertions failures to call abort(), + which will usually make debugging easier. + + MALLOC_FAILURE_ACTION default: sets errno to ENOMEM, or no-op on win32 + The action to take before "return 0" when malloc fails to be able to + return memory because there is none available. + + HAVE_MORECORE default: 1 (true) unless win32 or ONLY_MSPACES + True if this system supports sbrk or an emulation of it. + + MORECORE default: sbrk + The name of the sbrk-style system routine to call to obtain more + memory. See below for guidance on writing custom MORECORE + functions. The type of the argument to sbrk/MORECORE varies across + systems. It cannot be size_t, because it supports negative + arguments, so it is normally the signed type of the same width as + size_t (sometimes declared as "intptr_t"). It doesn't much matter + though. Internally, we only call it with arguments less than half + the max value of a size_t, which should work across all reasonable + possibilities, although sometimes generating compiler warnings. + + MORECORE_CONTIGUOUS default: 1 (true) if HAVE_MORECORE + If true, take advantage of fact that consecutive calls to MORECORE + with positive arguments always return contiguous increasing + addresses. This is true of unix sbrk. It does not hurt too much to + set it true anyway, since malloc copes with non-contiguities. + Setting it false when definitely non-contiguous saves time + and possibly wasted space it would take to discover this though. + + MORECORE_CANNOT_TRIM default: NOT defined + True if MORECORE cannot release space back to the system when given + negative arguments. This is generally necessary only if you are + using a hand-crafted MORECORE function that cannot handle negative + arguments. + + NO_SEGMENT_TRAVERSAL default: 0 + If non-zero, suppresses traversals of memory segments + returned by either MORECORE or CALL_MMAP. This disables + merging of segments that are contiguous, and selectively + releasing them to the OS if unused, but bounds execution times. + + HAVE_MMAP default: 1 (true) + True if this system supports mmap or an emulation of it. If so, and + HAVE_MORECORE is not true, MMAP is used for all system + allocation. If set and HAVE_MORECORE is true as well, MMAP is + primarily used to directly allocate very large blocks. It is also + used as a backup strategy in cases where MORECORE fails to provide + space from system. Note: A single call to MUNMAP is assumed to be + able to unmap memory that may have be allocated using multiple calls + to MMAP, so long as they are adjacent. + + HAVE_MREMAP default: 1 on linux, else 0 + If true realloc() uses mremap() to re-allocate large blocks and + extend or shrink allocation spaces. + + MMAP_CLEARS default: 1 except on WINCE. + True if mmap clears memory so calloc doesn't need to. This is true + for standard unix mmap using /dev/zero and on WIN32 except for WINCE. + + USE_BUILTIN_FFS default: 0 (i.e., not used) + Causes malloc to use the builtin ffs() function to compute indices. + Some compilers may recognize and intrinsify ffs to be faster than the + supplied C version. Also, the case of x86 using gcc is special-cased + to an asm instruction, so is already as fast as it can be, and so + this setting has no effect. Similarly for Win32 under recent MS compilers. + (On most x86s, the asm version is only slightly faster than the C version.) + + malloc_getpagesize default: derive from system includes, or 4096. + The system page size. To the extent possible, this malloc manages + memory from the system in page-size units. This may be (and + usually is) a function rather than a constant. This is ignored + if WIN32, where page size is determined using getSystemInfo during + initialization. + + USE_DEV_RANDOM default: 0 (i.e., not used) + Causes malloc to use /dev/random to initialize secure magic seed for + stamping footers. Otherwise, the current time is used. + + NO_MALLINFO default: 0 + If defined, don't compile "mallinfo". This can be a simple way + of dealing with mismatches between system declarations and + those in this file. + + MALLINFO_FIELD_TYPE default: size_t + The type of the fields in the mallinfo struct. This was originally + defined as "int" in SVID etc, but is more usefully defined as + size_t. The value is used only if HAVE_USR_INCLUDE_MALLOC_H is not set + + NO_MALLOC_STATS default: 0 + If defined, don't compile "malloc_stats". This avoids calls to + fprintf and bringing in stdio dependencies you might not want. + + REALLOC_ZERO_BYTES_FREES default: not defined + This should be set if a call to realloc with zero bytes should + be the same as a call to free. Some people think it should. Otherwise, + since this malloc returns a unique pointer for malloc(0), so does + realloc(p, 0). + + LACKS_UNISTD_H, LACKS_FCNTL_H, LACKS_SYS_PARAM_H, LACKS_SYS_MMAN_H + LACKS_STRINGS_H, LACKS_STRING_H, LACKS_SYS_TYPES_H, LACKS_ERRNO_H + LACKS_STDLIB_H LACKS_SCHED_H LACKS_TIME_H default: NOT defined unless on WIN32 + Define these if your system does not have these header files. + You might need to manually insert some of the declarations they provide. + + DEFAULT_GRANULARITY default: page size if MORECORE_CONTIGUOUS, + system_info.dwAllocationGranularity in WIN32, + otherwise 64K. + Also settable using mallopt(M_GRANULARITY, x) + The unit for allocating and deallocating memory from the system. On + most systems with contiguous MORECORE, there is no reason to + make this more than a page. However, systems with MMAP tend to + either require or encourage larger granularities. You can increase + this value to prevent system allocation functions to be called so + often, especially if they are slow. The value must be at least one + page and must be a power of two. Setting to 0 causes initialization + to either page size or win32 region size. (Note: In previous + versions of malloc, the equivalent of this option was called + "TOP_PAD") + + DEFAULT_TRIM_THRESHOLD default: 2MB + Also settable using mallopt(M_TRIM_THRESHOLD, x) + The maximum amount of unused top-most memory to keep before + releasing via malloc_trim in free(). Automatic trimming is mainly + useful in long-lived programs using contiguous MORECORE. Because + trimming via sbrk can be slow on some systems, and can sometimes be + wasteful (in cases where programs immediately afterward allocate + more large chunks) the value should be high enough so that your + overall system performance would improve by releasing this much + memory. As a rough guide, you might set to a value close to the + average size of a process (program) running on your system. + Releasing this much memory would allow such a process to run in + memory. Generally, it is worth tuning trim thresholds when a + program undergoes phases where several large chunks are allocated + and released in ways that can reuse each other's storage, perhaps + mixed with phases where there are no such chunks at all. The trim + value must be greater than page size to have any useful effect. To + disable trimming completely, you can set to MAX_SIZE_T. Note that the trick + some people use of mallocing a huge space and then freeing it at + program startup, in an attempt to reserve system memory, doesn't + have the intended effect under automatic trimming, since that memory + will immediately be returned to the system. + + DEFAULT_MMAP_THRESHOLD default: 256K + Also settable using mallopt(M_MMAP_THRESHOLD, x) + The request size threshold for using MMAP to directly service a + request. Requests of at least this size that cannot be allocated + using already-existing space will be serviced via mmap. (If enough + normal freed space already exists it is used instead.) Using mmap + segregates relatively large chunks of memory so that they can be + individually obtained and released from the host system. A request + serviced through mmap is never reused by any other request (at least + not directly; the system may just so happen to remap successive + requests to the same locations). Segregating space in this way has + the benefits that: Mmapped space can always be individually released + back to the system, which helps keep the system level memory demands + of a long-lived program low. Also, mapped memory doesn't become + `locked' between other chunks, as can happen with normally allocated + chunks, which means that even trimming via malloc_trim would not + release them. However, it has the disadvantage that the space + cannot be reclaimed, consolidated, and then used to service later + requests, as happens with normal chunks. The advantages of mmap + nearly always outweigh disadvantages for "large" chunks, but the + value of "large" may vary across systems. The default is an + empirically derived value that works well in most systems. You can + disable mmap by setting to MAX_SIZE_T. + + MAX_RELEASE_CHECK_RATE default: 4095 unless not HAVE_MMAP + The number of consolidated frees between checks to release + unused segments when freeing. When using non-contiguous segments, + especially with multiple mspaces, checking only for topmost space + doesn't always suffice to trigger trimming. To compensate for this, + free() will, with a period of MAX_RELEASE_CHECK_RATE (or the + current number of segments, if greater) try to release unused + segments to the OS when freeing chunks that result in + consolidation. The best value for this parameter is a compromise + between slowing down frees with relatively costly checks that + rarely trigger versus holding on to unused memory. To effectively + disable, set to MAX_SIZE_T. This may lead to a very slight speed + improvement at the expense of carrying around more memory. + */ /* Version identifier to allow people to support multiple versions */ #ifndef DLMALLOC_VERSION -#define DLMALLOC_VERSION 20804 +#define DLMALLOC_VERSION 20806 #endif /* DLMALLOC_VERSION */ +#ifndef DLMALLOC_EXPORT +#define DLMALLOC_EXPORT extern +#endif + #ifndef WIN32 #ifdef _WIN32 #define WIN32 1 @@ -503,6 +548,7 @@ MAX_RELEASE_CHECK_RATE default: 4095 unless not HAVE_MMAP #ifdef WIN32 #define WIN32_LEAN_AND_MEAN #include <windows.h> +#include <tchar.h> #define HAVE_MMAP 1 #define HAVE_MORECORE 0 #define LACKS_UNISTD_H @@ -512,14 +558,17 @@ MAX_RELEASE_CHECK_RATE default: 4095 unless not HAVE_MMAP #define LACKS_STRINGS_H #define LACKS_SYS_TYPES_H #define LACKS_ERRNO_H +#define LACKS_SCHED_H #ifndef MALLOC_FAILURE_ACTION #define MALLOC_FAILURE_ACTION #endif /* MALLOC_FAILURE_ACTION */ +#ifndef MMAP_CLEARS #ifdef _WIN32_WCE /* WINCE reportedly does not clear */ #define MMAP_CLEARS 0 #else #define MMAP_CLEARS 1 #endif /* _WIN32_WCE */ +#endif /*MMAP_CLEARS */ #endif /* WIN32 */ #if defined(DARWIN) || defined(_DARWIN) @@ -538,19 +587,31 @@ MAX_RELEASE_CHECK_RATE default: 4095 unless not HAVE_MMAP #include <sys/types.h> /* For size_t */ #endif /* LACKS_SYS_TYPES_H */ -#if (defined(__GNUC__) && ((defined(__i386__) || defined(__x86_64__)))) || (defined(_MSC_VER) && _MSC_VER>=1310) -#define SPIN_LOCKS_AVAILABLE 1 -#else -#define SPIN_LOCKS_AVAILABLE 0 -#endif - /* The maximum possible size_t value has all bits set */ #define MAX_SIZE_T (~(size_t)0) +#ifndef USE_LOCKS /* ensure true if spin or recursive locks set */ +#define USE_LOCKS ((defined(USE_SPIN_LOCKS) && USE_SPIN_LOCKS != 0) || \ +(defined(USE_RECURSIVE_LOCKS) && USE_RECURSIVE_LOCKS != 0)) +#endif /* USE_LOCKS */ + +#if USE_LOCKS /* Spin locks for gcc >= 4.1, older gcc on x86, MSC >= 1310 */ +#if ((defined(__GNUC__) && \ +((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)) || \ +defined(__i386__) || defined(__x86_64__))) || \ +(defined(_MSC_VER) && _MSC_VER>=1310)) +#ifndef USE_SPIN_LOCKS +#define USE_SPIN_LOCKS 1 +#endif /* USE_SPIN_LOCKS */ +#elif USE_SPIN_LOCKS +#error "USE_SPIN_LOCKS defined without implementation" +#endif /* ... locks available... */ +#elif !defined(USE_SPIN_LOCKS) +#define USE_SPIN_LOCKS 0 +#endif /* USE_LOCKS */ + #ifndef ONLY_MSPACES -#define ONLY_MSPACES 0 /* define to a value */ -#else -#define ONLY_MSPACES 1 +#define ONLY_MSPACES 0 #endif /* ONLY_MSPACES */ #ifndef MSPACES #if ONLY_MSPACES @@ -560,7 +621,7 @@ MAX_RELEASE_CHECK_RATE default: 4095 unless not HAVE_MMAP #endif /* ONLY_MSPACES */ #endif /* MSPACES */ #ifndef MALLOC_ALIGNMENT -#define MALLOC_ALIGNMENT ((size_t)8U) +#define MALLOC_ALIGNMENT ((size_t)(2 * sizeof(void *))) #endif /* MALLOC_ALIGNMENT */ #ifndef FOOTERS #define FOOTERS 0 @@ -574,24 +635,23 @@ MAX_RELEASE_CHECK_RATE default: 4095 unless not HAVE_MMAP #ifndef PROCEED_ON_ERROR #define PROCEED_ON_ERROR 0 #endif /* PROCEED_ON_ERROR */ -#ifndef USE_LOCKS -#define USE_LOCKS 0 -#endif /* USE_LOCKS */ -#ifndef USE_SPIN_LOCKS -#if USE_LOCKS && SPIN_LOCKS_AVAILABLE -#define USE_SPIN_LOCKS 1 -#else -#define USE_SPIN_LOCKS 0 -#endif /* USE_LOCKS && SPIN_LOCKS_AVAILABLE. */ -#endif /* USE_SPIN_LOCKS */ + #ifndef INSECURE #define INSECURE 0 #endif /* INSECURE */ +#ifndef MALLOC_INSPECT_ALL +#define MALLOC_INSPECT_ALL 0 +#endif /* MALLOC_INSPECT_ALL */ #ifndef HAVE_MMAP /* XXX Emscripten * mmap uses malloc, so malloc can't use mmap */ +#ifdef EMSCRIPTEN #define HAVE_MMAP 0 +#else +#define HAVE_MMAP 1 +#endif + #endif /* HAVE_MMAP */ #ifndef MMAP_CLEARS #define MMAP_CLEARS 1 @@ -599,6 +659,7 @@ MAX_RELEASE_CHECK_RATE default: 4095 unless not HAVE_MMAP #ifndef HAVE_MREMAP #ifdef linux #define HAVE_MREMAP 1 +#define _GNU_SOURCE /* Turns on mremap() definition */ #else /* linux */ #define HAVE_MREMAP 0 #endif /* linux */ @@ -661,16 +722,19 @@ MAX_RELEASE_CHECK_RATE default: 4095 unless not HAVE_MMAP #ifndef MALLINFO_FIELD_TYPE #define MALLINFO_FIELD_TYPE size_t #endif /* MALLINFO_FIELD_TYPE */ +#ifndef NO_MALLOC_STATS +#define NO_MALLOC_STATS 0 +#endif /* NO_MALLOC_STATS */ #ifndef NO_SEGMENT_TRAVERSAL #define NO_SEGMENT_TRAVERSAL 0 #endif /* NO_SEGMENT_TRAVERSAL */ /* - mallopt tuning options. SVID/XPG defines four standard parameter - numbers for mallopt, normally defined in malloc.h. None of these - are used in this malloc, so setting them has no effect. But this - malloc does support the following options. -*/ + mallopt tuning options. SVID/XPG defines four standard parameter + numbers for mallopt, normally defined in malloc.h. None of these + are used in this malloc, so setting them has no effect. But this + malloc does support the following options. + */ #define M_TRIM_THRESHOLD (-1) #define M_GRANULARITY (-2) @@ -680,26 +744,26 @@ MAX_RELEASE_CHECK_RATE default: 4095 unless not HAVE_MMAP #if !NO_MALLINFO /* - This version of malloc supports the standard SVID/XPG mallinfo - routine that returns a struct containing usage properties and - statistics. It should work on any system that has a - /usr/include/malloc.h defining struct mallinfo. The main - declaration needed is the mallinfo struct that is returned (by-copy) - by mallinfo(). The malloinfo struct contains a bunch of fields that - are not even meaningful in this version of malloc. These fields are - are instead filled by mallinfo() with other numbers that might be of - interest. - - HAVE_USR_INCLUDE_MALLOC_H should be set if you have a - /usr/include/malloc.h file that includes a declaration of struct - mallinfo. If so, it is included; else a compliant version is - declared below. These must be precisely the same for mallinfo() to - work. The original SVID version of this struct, defined on most - systems with mallinfo, declares all fields as ints. But some others - define as unsigned long. If your system defines the fields using a - type of different width than listed here, you MUST #include your - system version and #define HAVE_USR_INCLUDE_MALLOC_H. -*/ + This version of malloc supports the standard SVID/XPG mallinfo + routine that returns a struct containing usage properties and + statistics. It should work on any system that has a + /usr/include/malloc.h defining struct mallinfo. The main + declaration needed is the mallinfo struct that is returned (by-copy) + by mallinfo(). The malloinfo struct contains a bunch of fields that + are not even meaningful in this version of malloc. These fields are + are instead filled by mallinfo() with other numbers that might be of + interest. + + HAVE_USR_INCLUDE_MALLOC_H should be set if you have a + /usr/include/malloc.h file that includes a declaration of struct + mallinfo. If so, it is included; else a compliant version is + declared below. These must be precisely the same for mallinfo() to + work. The original SVID version of this struct, defined on most + systems with mallinfo, declares all fields as ints. But some others + define as unsigned long. If your system defines the fields using a + type of different width than listed here, you MUST #include your + system version and #define HAVE_USR_INCLUDE_MALLOC_H. + */ /* #define HAVE_USR_INCLUDE_MALLOC_H */ @@ -707,65 +771,69 @@ MAX_RELEASE_CHECK_RATE default: 4095 unless not HAVE_MMAP #include "/usr/include/malloc.h" #else /* HAVE_USR_INCLUDE_MALLOC_H */ #ifndef STRUCT_MALLINFO_DECLARED +/* HP-UX (and others?) redefines mallinfo unless _STRUCT_MALLINFO is defined */ +#define _STRUCT_MALLINFO #define STRUCT_MALLINFO_DECLARED 1 struct mallinfo { - MALLINFO_FIELD_TYPE arena; /* non-mmapped space allocated from system */ - MALLINFO_FIELD_TYPE ordblks; /* number of free chunks */ - MALLINFO_FIELD_TYPE smblks; /* always 0 */ - MALLINFO_FIELD_TYPE hblks; /* always 0 */ - MALLINFO_FIELD_TYPE hblkhd; /* space in mmapped regions */ - MALLINFO_FIELD_TYPE usmblks; /* maximum total allocated space */ - MALLINFO_FIELD_TYPE fsmblks; /* always 0 */ - MALLINFO_FIELD_TYPE uordblks; /* total allocated space */ - MALLINFO_FIELD_TYPE fordblks; /* total free space */ - MALLINFO_FIELD_TYPE keepcost; /* releasable (via malloc_trim) space */ + MALLINFO_FIELD_TYPE arena; /* non-mmapped space allocated from system */ + MALLINFO_FIELD_TYPE ordblks; /* number of free chunks */ + MALLINFO_FIELD_TYPE smblks; /* always 0 */ + MALLINFO_FIELD_TYPE hblks; /* always 0 */ + MALLINFO_FIELD_TYPE hblkhd; /* space in mmapped regions */ + MALLINFO_FIELD_TYPE usmblks; /* maximum total allocated space */ + MALLINFO_FIELD_TYPE fsmblks; /* always 0 */ + MALLINFO_FIELD_TYPE uordblks; /* total allocated space */ + MALLINFO_FIELD_TYPE fordblks; /* total free space */ + MALLINFO_FIELD_TYPE keepcost; /* releasable (via malloc_trim) space */ }; #endif /* STRUCT_MALLINFO_DECLARED */ #endif /* HAVE_USR_INCLUDE_MALLOC_H */ #endif /* NO_MALLINFO */ /* - Try to persuade compilers to inline. The most critical functions for - inlining are defined as macros, so these aren't used for them. -*/ + Try to persuade compilers to inline. The most critical functions for + inlining are defined as macros, so these aren't used for them. + */ #ifndef FORCEINLINE - #if defined(__GNUC__) +#if defined(__GNUC__) #define FORCEINLINE __inline __attribute__ ((always_inline)) - #elif defined(_MSC_VER) - #define FORCEINLINE __forceinline - #endif +#elif defined(_MSC_VER) +#define FORCEINLINE __forceinline +#endif #endif #ifndef NOINLINE - #if defined(__GNUC__) - #define NOINLINE __attribute__ ((noinline)) - #elif defined(_MSC_VER) - #define NOINLINE __declspec(noinline) - #else - #define NOINLINE - #endif +#if defined(__GNUC__) +#define NOINLINE __attribute__ ((noinline)) +#elif defined(_MSC_VER) +#define NOINLINE __declspec(noinline) +#else +#define NOINLINE +#endif #endif #ifdef __cplusplus extern "C" { #ifndef FORCEINLINE - #define FORCEINLINE inline +#define FORCEINLINE inline #endif #endif /* __cplusplus */ #ifndef FORCEINLINE - #define FORCEINLINE +#define FORCEINLINE #endif - + #if !ONLY_MSPACES - -/* ------------------- Declarations of public routines ------------------- */ - + + /* ------------------- Declarations of public routines ------------------- */ + #ifndef USE_DL_PREFIX #define dlcalloc calloc #define dlfree free #define dlmalloc malloc #define dlmemalign memalign +#define dlposix_memalign posix_memalign #define dlrealloc realloc +#define dlrealloc_in_place realloc_in_place #define dlvalloc valloc #define dlpvalloc pvalloc #define dlmallinfo mallinfo @@ -775,533 +843,615 @@ extern "C" { #define dlmalloc_usable_size malloc_usable_size #define dlmalloc_footprint malloc_footprint #define dlmalloc_max_footprint malloc_max_footprint +#define dlmalloc_footprint_limit malloc_footprint_limit +#define dlmalloc_set_footprint_limit malloc_set_footprint_limit +#define dlmalloc_inspect_all malloc_inspect_all #define dlindependent_calloc independent_calloc #define dlindependent_comalloc independent_comalloc +#define dlbulk_free bulk_free #endif /* USE_DL_PREFIX */ - - -/* - malloc(size_t n) - Returns a pointer to a newly allocated chunk of at least n bytes, or - null if no space is available, in which case errno is set to ENOMEM - on ANSI C systems. - - If n is zero, malloc returns a minimum-sized chunk. (The minimum - size is 16 bytes on most 32bit systems, and 32 bytes on 64bit - systems.) Note that size_t is an unsigned type, so calls with - arguments that would be negative if signed are interpreted as - requests for huge amounts of space, which will often fail. The - maximum supported value of n differs across systems, but is in all - cases less than the maximum representable value of a size_t. -*/ -void* dlmalloc(size_t) __THROW __attribute_malloc__ __wur; - -/* - free(void* p) - Releases the chunk of memory pointed to by p, that had been previously - allocated using malloc or a related routine such as realloc. - It has no effect if p is null. If p was not malloced or already - freed, free(p) will by default cause the current program to abort. -*/ -void dlfree(void*) __THROW __wur; - -/* - calloc(size_t n_elements, size_t element_size); - Returns a pointer to n_elements * element_size bytes, with all locations - set to zero. -*/ -void* dlcalloc(size_t, size_t) __THROW __attribute_malloc__ __wur; - -/* - realloc(void* p, size_t n) - Returns a pointer to a chunk of size n that contains the same data - as does chunk p up to the minimum of (n, p's size) bytes, or null - if no space is available. - - The returned pointer may or may not be the same as p. The algorithm - prefers extending p in most cases when possible, otherwise it - employs the equivalent of a malloc-copy-free sequence. - - If p is null, realloc is equivalent to malloc. - - If space is not available, realloc returns null, errno is set (if on - ANSI) and p is NOT freed. - - if n is for fewer bytes than already held by p, the newly unused - space is lopped off and freed if possible. realloc with a size - argument of zero (re)allocates a minimum-sized chunk. - - The old unix realloc convention of allowing the last-free'd chunk - to be used as an argument to realloc is not supported. -*/ - -void* dlrealloc(void*, size_t) __THROW __attribute_malloc__ __wur; - -/* - memalign(size_t alignment, size_t n); - Returns a pointer to a newly allocated chunk of n bytes, aligned - in accord with the alignment argument. - - The alignment argument should be a power of two. If the argument is - not a power of two, the nearest greater power is used. - 8-byte alignment is guaranteed by normal malloc calls, so don't - bother calling memalign with an argument of 8 or less. - - Overreliance on memalign is a sure way to fragment space. -*/ -void* dlmemalign(size_t, size_t); - -/* - valloc(size_t n); - Equivalent to memalign(pagesize, n), where pagesize is the page - size of the system. If the pagesize is unknown, 4096 is used. -*/ -void* dlvalloc(size_t) __THROW __attribute_malloc__ __wur; - -/* - mallopt(int parameter_number, int parameter_value) - Sets tunable parameters The format is to provide a - (parameter-number, parameter-value) pair. mallopt then sets the - corresponding parameter to the argument value if it can (i.e., so - long as the value is meaningful), and returns 1 if successful else - 0. To workaround the fact that mallopt is specified to use int, - not size_t parameters, the value -1 is specially treated as the - maximum unsigned size_t value. - - SVID/XPG/ANSI defines four standard param numbers for mallopt, - normally defined in malloc.h. None of these are use in this malloc, - so setting them has no effect. But this malloc also supports other - options in mallopt. See below for details. Briefly, supported - parameters are as follows (listed defaults are for "typical" - configurations). - - Symbol param # default allowed param values - M_TRIM_THRESHOLD -1 2*1024*1024 any (-1 disables) - M_GRANULARITY -2 page size any power of 2 >= page size - M_MMAP_THRESHOLD -3 256*1024 any (or 0 if no MMAP support) -*/ -int dlmallopt(int, int); - -/* - malloc_footprint(); - Returns the number of bytes obtained from the system. The total - number of bytes allocated by malloc, realloc etc., is less than this - value. Unlike mallinfo, this function returns only a precomputed - result, so can be called frequently to monitor memory consumption. - Even if locks are otherwise defined, this function does not use them, - so results might not be up to date. -*/ -size_t dlmalloc_footprint(void); - -/* - malloc_max_footprint(); - Returns the maximum number of bytes obtained from the system. This - value will be greater than current footprint if deallocated space - has been reclaimed by the system. The peak number of bytes allocated - by malloc, realloc etc., is less than this value. Unlike mallinfo, - this function returns only a precomputed result, so can be called - frequently to monitor memory consumption. Even if locks are - otherwise defined, this function does not use them, so results might - not be up to date. -*/ -size_t dlmalloc_max_footprint(void); - + + /* + malloc(size_t n) + Returns a pointer to a newly allocated chunk of at least n bytes, or + null if no space is available, in which case errno is set to ENOMEM + on ANSI C systems. + + If n is zero, malloc returns a minimum-sized chunk. (The minimum + size is 16 bytes on most 32bit systems, and 32 bytes on 64bit + systems.) Note that size_t is an unsigned type, so calls with + arguments that would be negative if signed are interpreted as + requests for huge amounts of space, which will often fail. The + maximum supported value of n differs across systems, but is in all + cases less than the maximum representable value of a size_t. + */ + DLMALLOC_EXPORT void* dlmalloc(size_t); + + /* + free(void* p) + Releases the chunk of memory pointed to by p, that had been previously + allocated using malloc or a related routine such as realloc. + It has no effect if p is null. If p was not malloced or already + freed, free(p) will by default cause the current program to abort. + */ + DLMALLOC_EXPORT void dlfree(void*); + + /* + calloc(size_t n_elements, size_t element_size); + Returns a pointer to n_elements * element_size bytes, with all locations + set to zero. + */ + DLMALLOC_EXPORT void* dlcalloc(size_t, size_t); + + /* + realloc(void* p, size_t n) + Returns a pointer to a chunk of size n that contains the same data + as does chunk p up to the minimum of (n, p's size) bytes, or null + if no space is available. + + The returned pointer may or may not be the same as p. The algorithm + prefers extending p in most cases when possible, otherwise it + employs the equivalent of a malloc-copy-free sequence. + + If p is null, realloc is equivalent to malloc. + + If space is not available, realloc returns null, errno is set (if on + ANSI) and p is NOT freed. + + if n is for fewer bytes than already held by p, the newly unused + space is lopped off and freed if possible. realloc with a size + argument of zero (re)allocates a minimum-sized chunk. + + The old unix realloc convention of allowing the last-free'd chunk + to be used as an argument to realloc is not supported. + */ + DLMALLOC_EXPORT void* dlrealloc(void*, size_t); + + /* + realloc_in_place(void* p, size_t n) + Resizes the space allocated for p to size n, only if this can be + done without moving p (i.e., only if there is adjacent space + available if n is greater than p's current allocated size, or n is + less than or equal to p's size). This may be used instead of plain + realloc if an alternative allocation strategy is needed upon failure + to expand space; for example, reallocation of a buffer that must be + memory-aligned or cleared. You can use realloc_in_place to trigger + these alternatives only when needed. + + Returns p if successful; otherwise null. + */ + DLMALLOC_EXPORT void* dlrealloc_in_place(void*, size_t); + + /* + memalign(size_t alignment, size_t n); + Returns a pointer to a newly allocated chunk of n bytes, aligned + in accord with the alignment argument. + + The alignment argument should be a power of two. If the argument is + not a power of two, the nearest greater power is used. + 8-byte alignment is guaranteed by normal malloc calls, so don't + bother calling memalign with an argument of 8 or less. + + Overreliance on memalign is a sure way to fragment space. + */ + DLMALLOC_EXPORT void* dlmemalign(size_t, size_t); + + /* + int posix_memalign(void** pp, size_t alignment, size_t n); + Allocates a chunk of n bytes, aligned in accord with the alignment + argument. Differs from memalign only in that it (1) assigns the + allocated memory to *pp rather than returning it, (2) fails and + returns EINVAL if the alignment is not a power of two (3) fails and + returns ENOMEM if memory cannot be allocated. + */ + DLMALLOC_EXPORT int dlposix_memalign(void**, size_t, size_t); + + /* + valloc(size_t n); + Equivalent to memalign(pagesize, n), where pagesize is the page + size of the system. If the pagesize is unknown, 4096 is used. + */ + DLMALLOC_EXPORT void* dlvalloc(size_t); + + /* + mallopt(int parameter_number, int parameter_value) + Sets tunable parameters The format is to provide a + (parameter-number, parameter-value) pair. mallopt then sets the + corresponding parameter to the argument value if it can (i.e., so + long as the value is meaningful), and returns 1 if successful else + 0. To workaround the fact that mallopt is specified to use int, + not size_t parameters, the value -1 is specially treated as the + maximum unsigned size_t value. + + SVID/XPG/ANSI defines four standard param numbers for mallopt, + normally defined in malloc.h. None of these are use in this malloc, + so setting them has no effect. But this malloc also supports other + options in mallopt. See below for details. Briefly, supported + parameters are as follows (listed defaults are for "typical" + configurations). + + Symbol param # default allowed param values + M_TRIM_THRESHOLD -1 2*1024*1024 any (-1 disables) + M_GRANULARITY -2 page size any power of 2 >= page size + M_MMAP_THRESHOLD -3 256*1024 any (or 0 if no MMAP support) + */ + DLMALLOC_EXPORT int dlmallopt(int, int); + + /* + malloc_footprint(); + Returns the number of bytes obtained from the system. The total + number of bytes allocated by malloc, realloc etc., is less than this + value. Unlike mallinfo, this function returns only a precomputed + result, so can be called frequently to monitor memory consumption. + Even if locks are otherwise defined, this function does not use them, + so results might not be up to date. + */ + DLMALLOC_EXPORT size_t dlmalloc_footprint(void); + + /* + malloc_max_footprint(); + Returns the maximum number of bytes obtained from the system. This + value will be greater than current footprint if deallocated space + has been reclaimed by the system. The peak number of bytes allocated + by malloc, realloc etc., is less than this value. Unlike mallinfo, + this function returns only a precomputed result, so can be called + frequently to monitor memory consumption. Even if locks are + otherwise defined, this function does not use them, so results might + not be up to date. + */ + DLMALLOC_EXPORT size_t dlmalloc_max_footprint(void); + + /* + malloc_footprint_limit(); + Returns the number of bytes that the heap is allowed to obtain from + the system, returning the last value returned by + malloc_set_footprint_limit, or the maximum size_t value if + never set. The returned value reflects a permission. There is no + guarantee that this number of bytes can actually be obtained from + the system. + */ + DLMALLOC_EXPORT size_t dlmalloc_footprint_limit(); + + /* + malloc_set_footprint_limit(); + Sets the maximum number of bytes to obtain from the system, causing + failure returns from malloc and related functions upon attempts to + exceed this value. The argument value may be subject to page + rounding to an enforceable limit; this actual value is returned. + Using an argument of the maximum possible size_t effectively + disables checks. If the argument is less than or equal to the + current malloc_footprint, then all future allocations that require + additional system memory will fail. However, invocation cannot + retroactively deallocate existing used memory. + */ + DLMALLOC_EXPORT size_t dlmalloc_set_footprint_limit(size_t bytes); + +#if MALLOC_INSPECT_ALL + /* + malloc_inspect_all(void(*handler)(void *start, + void *end, + size_t used_bytes, + void* callback_arg), + void* arg); + Traverses the heap and calls the given handler for each managed + region, skipping all bytes that are (or may be) used for bookkeeping + purposes. Traversal does not include include chunks that have been + directly memory mapped. Each reported region begins at the start + address, and continues up to but not including the end address. The + first used_bytes of the region contain allocated data. If + used_bytes is zero, the region is unallocated. The handler is + invoked with the given callback argument. If locks are defined, they + are held during the entire traversal. It is a bad idea to invoke + other malloc functions from within the handler. + + For example, to count the number of in-use chunks with size greater + than 1000, you could write: + static int count = 0; + void count_chunks(void* start, void* end, size_t used, void* arg) { + if (used >= 1000) ++count; + } + then: + malloc_inspect_all(count_chunks, NULL); + + malloc_inspect_all is compiled only if MALLOC_INSPECT_ALL is defined. + */ + DLMALLOC_EXPORT void dlmalloc_inspect_all(void(*handler)(void*, void *, size_t, void*), + void* arg); + +#endif /* MALLOC_INSPECT_ALL */ + #if !NO_MALLINFO -/* - mallinfo() - Returns (by copy) a struct containing various summary statistics: - - arena: current total non-mmapped bytes allocated from system - ordblks: the number of free chunks - smblks: always zero. - hblks: current number of mmapped regions - hblkhd: total bytes held in mmapped regions - usmblks: the maximum total allocated space. This will be greater - than current total if trimming has occurred. - fsmblks: always zero - uordblks: current total allocated space (normal or mmapped) - fordblks: total free space - keepcost: the maximum number of bytes that could ideally be released - back to system via malloc_trim. ("ideally" means that - it ignores page restrictions etc.) - - Because these fields are ints, but internal bookkeeping may - be kept as longs, the reported values may wrap around zero and - thus be inaccurate. -*/ -struct mallinfo dlmallinfo(void); + /* + mallinfo() + Returns (by copy) a struct containing various summary statistics: + + arena: current total non-mmapped bytes allocated from system + ordblks: the number of free chunks + smblks: always zero. + hblks: current number of mmapped regions + hblkhd: total bytes held in mmapped regions + usmblks: the maximum total allocated space. This will be greater + than current total if trimming has occurred. + fsmblks: always zero + uordblks: current total allocated space (normal or mmapped) + fordblks: total free space + keepcost: the maximum number of bytes that could ideally be released + back to system via malloc_trim. ("ideally" means that + it ignores page restrictions etc.) + + Because these fields are ints, but internal bookkeeping may + be kept as longs, the reported values may wrap around zero and + thus be inaccurate. + */ + DLMALLOC_EXPORT struct mallinfo dlmallinfo(void); #endif /* NO_MALLINFO */ - -/* - independent_calloc(size_t n_elements, size_t element_size, void* chunks[]); - - independent_calloc is similar to calloc, but instead of returning a - single cleared space, it returns an array of pointers to n_elements - independent elements that can hold contents of size elem_size, each - of which starts out cleared, and can be independently freed, - realloc'ed etc. The elements are guaranteed to be adjacently - allocated (this is not guaranteed to occur with multiple callocs or - mallocs), which may also improve cache locality in some - applications. - - The "chunks" argument is optional (i.e., may be null, which is - probably the most typical usage). If it is null, the returned array - is itself dynamically allocated and should also be freed when it is - no longer needed. Otherwise, the chunks array must be of at least - n_elements in length. It is filled in with the pointers to the - chunks. - - In either case, independent_calloc returns this pointer array, or - null if the allocation failed. If n_elements is zero and "chunks" - is null, it returns a chunk representing an array with zero elements - (which should be freed if not wanted). - - Each element must be individually freed when it is no longer - needed. If you'd like to instead be able to free all at once, you - should instead use regular calloc and assign pointers into this - space to represent elements. (In this case though, you cannot - independently free elements.) - - independent_calloc simplifies and speeds up implementations of many - kinds of pools. It may also be useful when constructing large data - structures that initially have a fixed number of fixed-sized nodes, - but the number is not known at compile time, and some of the nodes - may later need to be freed. For example: - - struct Node { int item; struct Node* next; }; - - struct Node* build_list() { - struct Node** pool; - int n = read_number_of_nodes_needed(); - if (n <= 0) return 0; - pool = (struct Node**)(independent_calloc(n, sizeof(struct Node), 0); - if (pool == 0) die(); - // organize into a linked list... - struct Node* first = pool[0]; - for (i = 0; i < n-1; ++i) - pool[i]->next = pool[i+1]; - free(pool); // Can now free the array (or not, if it is needed later) - return first; - } -*/ -void** dlindependent_calloc(size_t, size_t, void**); - -/* - independent_comalloc(size_t n_elements, size_t sizes[], void* chunks[]); - - independent_comalloc allocates, all at once, a set of n_elements - chunks with sizes indicated in the "sizes" array. It returns - an array of pointers to these elements, each of which can be - independently freed, realloc'ed etc. The elements are guaranteed to - be adjacently allocated (this is not guaranteed to occur with - multiple callocs or mallocs), which may also improve cache locality - in some applications. - - The "chunks" argument is optional (i.e., may be null). If it is null - the returned array is itself dynamically allocated and should also - be freed when it is no longer needed. Otherwise, the chunks array - must be of at least n_elements in length. It is filled in with the - pointers to the chunks. - - In either case, independent_comalloc returns this pointer array, or - null if the allocation failed. If n_elements is zero and chunks is - null, it returns a chunk representing an array with zero elements - (which should be freed if not wanted). - - Each element must be individually freed when it is no longer - needed. If you'd like to instead be able to free all at once, you - should instead use a single regular malloc, and assign pointers at - particular offsets in the aggregate space. (In this case though, you - cannot independently free elements.) - - independent_comallac differs from independent_calloc in that each - element may have a different size, and also that it does not - automatically clear elements. - - independent_comalloc can be used to speed up allocation in cases - where several structs or objects must always be allocated at the - same time. For example: - - struct Head { ... } - struct Foot { ... } - - void send_message(char* msg) { - int msglen = strlen(msg); - size_t sizes[3] = { sizeof(struct Head), msglen, sizeof(struct Foot) }; - void* chunks[3]; - if (independent_comalloc(3, sizes, chunks) == 0) - die(); - struct Head* head = (struct Head*)(chunks[0]); - char* body = (char*)(chunks[1]); - struct Foot* foot = (struct Foot*)(chunks[2]); - // ... - } - - In general though, independent_comalloc is worth using only for - larger values of n_elements. For small values, you probably won't - detect enough difference from series of malloc calls to bother. - - Overuse of independent_comalloc can increase overall memory usage, - since it cannot reuse existing noncontiguous small chunks that - might be available for some of the elements. -*/ -void** dlindependent_comalloc(size_t, size_t*, void**); - - -/* - pvalloc(size_t n); - Equivalent to valloc(minimum-page-that-holds(n)), that is, - round up n to nearest pagesize. - */ -void* dlpvalloc(size_t); - -/* - malloc_trim(size_t pad); - - If possible, gives memory back to the system (via negative arguments - to sbrk) if there is unused memory at the `high' end of the malloc - pool or in unused MMAP segments. You can call this after freeing - large blocks of memory to potentially reduce the system-level memory - requirements of a program. However, it cannot guarantee to reduce - memory. Under some allocation patterns, some large free blocks of - memory will be locked between two used chunks, so they cannot be - given back to the system. - - The `pad' argument to malloc_trim represents the amount of free - trailing space to leave untrimmed. If this argument is zero, only - the minimum amount of memory to maintain internal data structures - will be left. Non-zero arguments can be supplied to maintain enough - trailing space to service future expected allocations without having - to re-obtain memory from the system. - - Malloc_trim returns 1 if it actually released any memory, else 0. -*/ -int dlmalloc_trim(size_t); - -/* - malloc_stats(); - Prints on stderr the amount of space obtained from the system (both - via sbrk and mmap), the maximum amount (which may be more than - current if malloc_trim and/or munmap got called), and the current - number of bytes allocated via malloc (or realloc, etc) but not yet - freed. Note that this is the number of bytes allocated, not the - number requested. It will be larger than the number requested - because of alignment and bookkeeping overhead. Because it includes - alignment wastage as being in use, this figure may be greater than - zero even when no user-level chunks are allocated. - - The reported current and maximum system memory can be inaccurate if - a program makes other calls to system memory allocation functions - (normally sbrk) outside of malloc. - - malloc_stats prints only the most commonly interesting statistics. - More information can be obtained by calling mallinfo. -*/ -void dlmalloc_stats(void); - + + /* + independent_calloc(size_t n_elements, size_t element_size, void* chunks[]); + + independent_calloc is similar to calloc, but instead of returning a + single cleared space, it returns an array of pointers to n_elements + independent elements that can hold contents of size elem_size, each + of which starts out cleared, and can be independently freed, + realloc'ed etc. The elements are guaranteed to be adjacently + allocated (this is not guaranteed to occur with multiple callocs or + mallocs), which may also improve cache locality in some + applications. + + The "chunks" argument is optional (i.e., may be null, which is + probably the most typical usage). If it is null, the returned array + is itself dynamically allocated and should also be freed when it is + no longer needed. Otherwise, the chunks array must be of at least + n_elements in length. It is filled in with the pointers to the + chunks. + + In either case, independent_calloc returns this pointer array, or + null if the allocation failed. If n_elements is zero and "chunks" + is null, it returns a chunk representing an array with zero elements + (which should be freed if not wanted). + + Each element must be freed when it is no longer needed. This can be + done all at once using bulk_free. + + independent_calloc simplifies and speeds up implementations of many + kinds of pools. It may also be useful when constructing large data + structures that initially have a fixed number of fixed-sized nodes, + but the number is not known at compile time, and some of the nodes + may later need to be freed. For example: + + struct Node { int item; struct Node* next; }; + + struct Node* build_list() { + struct Node** pool; + int n = read_number_of_nodes_needed(); + if (n <= 0) return 0; + pool = (struct Node**)(independent_calloc(n, sizeof(struct Node), 0); + if (pool == 0) die(); + // organize into a linked list... + struct Node* first = pool[0]; + for (i = 0; i < n-1; ++i) + pool[i]->next = pool[i+1]; + free(pool); // Can now free the array (or not, if it is needed later) + return first; + } + */ + DLMALLOC_EXPORT void** dlindependent_calloc(size_t, size_t, void**); + + /* + independent_comalloc(size_t n_elements, size_t sizes[], void* chunks[]); + + independent_comalloc allocates, all at once, a set of n_elements + chunks with sizes indicated in the "sizes" array. It returns + an array of pointers to these elements, each of which can be + independently freed, realloc'ed etc. The elements are guaranteed to + be adjacently allocated (this is not guaranteed to occur with + multiple callocs or mallocs), which may also improve cache locality + in some applications. + + The "chunks" argument is optional (i.e., may be null). If it is null + the returned array is itself dynamically allocated and should also + be freed when it is no longer needed. Otherwise, the chunks array + must be of at least n_elements in length. It is filled in with the + pointers to the chunks. + + In either case, independent_comalloc returns this pointer array, or + null if the allocation failed. If n_elements is zero and chunks is + null, it returns a chunk representing an array with zero elements + (which should be freed if not wanted). + + Each element must be freed when it is no longer needed. This can be + done all at once using bulk_free. + + independent_comallac differs from independent_calloc in that each + element may have a different size, and also that it does not + automatically clear elements. + + independent_comalloc can be used to speed up allocation in cases + where several structs or objects must always be allocated at the + same time. For example: + + struct Head { ... } + struct Foot { ... } + + void send_message(char* msg) { + int msglen = strlen(msg); + size_t sizes[3] = { sizeof(struct Head), msglen, sizeof(struct Foot) }; + void* chunks[3]; + if (independent_comalloc(3, sizes, chunks) == 0) + die(); + struct Head* head = (struct Head*)(chunks[0]); + char* body = (char*)(chunks[1]); + struct Foot* foot = (struct Foot*)(chunks[2]); + // ... + } + + In general though, independent_comalloc is worth using only for + larger values of n_elements. For small values, you probably won't + detect enough difference from series of malloc calls to bother. + + Overuse of independent_comalloc can increase overall memory usage, + since it cannot reuse existing noncontiguous small chunks that + might be available for some of the elements. + */ + DLMALLOC_EXPORT void** dlindependent_comalloc(size_t, size_t*, void**); + + /* + bulk_free(void* array[], size_t n_elements) + Frees and clears (sets to null) each non-null pointer in the given + array. This is likely to be faster than freeing them one-by-one. + If footers are used, pointers that have been allocated in different + mspaces are not freed or cleared, and the count of all such pointers + is returned. For large arrays of pointers with poor locality, it + may be worthwhile to sort this array before calling bulk_free. + */ + DLMALLOC_EXPORT size_t dlbulk_free(void**, size_t n_elements); + + /* + pvalloc(size_t n); + Equivalent to valloc(minimum-page-that-holds(n)), that is, + round up n to nearest pagesize. + */ + DLMALLOC_EXPORT void* dlpvalloc(size_t); + + /* + malloc_trim(size_t pad); + + If possible, gives memory back to the system (via negative arguments + to sbrk) if there is unused memory at the `high' end of the malloc + pool or in unused MMAP segments. You can call this after freeing + large blocks of memory to potentially reduce the system-level memory + requirements of a program. However, it cannot guarantee to reduce + memory. Under some allocation patterns, some large free blocks of + memory will be locked between two used chunks, so they cannot be + given back to the system. + + The `pad' argument to malloc_trim represents the amount of free + trailing space to leave untrimmed. If this argument is zero, only + the minimum amount of memory to maintain internal data structures + will be left. Non-zero arguments can be supplied to maintain enough + trailing space to service future expected allocations without having + to re-obtain memory from the system. + + Malloc_trim returns 1 if it actually released any memory, else 0. + */ + DLMALLOC_EXPORT int dlmalloc_trim(size_t); + + /* + malloc_stats(); + Prints on stderr the amount of space obtained from the system (both + via sbrk and mmap), the maximum amount (which may be more than + current if malloc_trim and/or munmap got called), and the current + number of bytes allocated via malloc (or realloc, etc) but not yet + freed. Note that this is the number of bytes allocated, not the + number requested. It will be larger than the number requested + because of alignment and bookkeeping overhead. Because it includes + alignment wastage as being in use, this figure may be greater than + zero even when no user-level chunks are allocated. + + The reported current and maximum system memory can be inaccurate if + a program makes other calls to system memory allocation functions + (normally sbrk) outside of malloc. + + malloc_stats prints only the most commonly interesting statistics. + More information can be obtained by calling mallinfo. + */ + DLMALLOC_EXPORT void dlmalloc_stats(void); + + /* + malloc_usable_size(void* p); + + Returns the number of bytes you can actually use in + an allocated chunk, which may be more than you requested (although + often not) due to alignment and minimum size constraints. + You can use this many bytes without worrying about + overwriting other allocated objects. This is not a particularly great + programming practice. malloc_usable_size can be more useful in + debugging and assertions, for example: + + p = malloc(n); + assert(malloc_usable_size(p) >= 256); + */ + size_t dlmalloc_usable_size(void*); + #endif /* ONLY_MSPACES */ - -/* - malloc_usable_size(void* p); - - Returns the number of bytes you can actually use in - an allocated chunk, which may be more than you requested (although - often not) due to alignment and minimum size constraints. - You can use this many bytes without worrying about - overwriting other allocated objects. This is not a particularly great - programming practice. malloc_usable_size can be more useful in - debugging and assertions, for example: - - p = malloc(n); - assert(malloc_usable_size(p) >= 256); -*/ -size_t dlmalloc_usable_size(void*); - - + #if MSPACES - -/* - mspace is an opaque type representing an independent - region of space that supports mspace_malloc, etc. -*/ -typedef void* mspace; - -/* - create_mspace creates and returns a new independent space with the - given initial capacity, or, if 0, the default granularity size. It - returns null if there is no system memory available to create the - space. If argument locked is non-zero, the space uses a separate - lock to control access. The capacity of the space will grow - dynamically as needed to service mspace_malloc requests. You can - control the sizes of incremental increases of this space by - compiling with a different DEFAULT_GRANULARITY or dynamically - setting with mallopt(M_GRANULARITY, value). -*/ -mspace create_mspace(size_t capacity, int locked); - -/* - destroy_mspace destroys the given space, and attempts to return all - of its memory back to the system, returning the total number of - bytes freed. After destruction, the results of access to all memory - used by the space become undefined. -*/ -size_t destroy_mspace(mspace msp); - -/* - create_mspace_with_base uses the memory supplied as the initial base - of a new mspace. Part (less than 128*sizeof(size_t) bytes) of this - space is used for bookkeeping, so the capacity must be at least this - large. (Otherwise 0 is returned.) When this initial space is - exhausted, additional memory will be obtained from the system. - Destroying this space will deallocate all additionally allocated - space (if possible) but not the initial base. -*/ -mspace create_mspace_with_base(void* base, size_t capacity, int locked); - -/* - mspace_track_large_chunks controls whether requests for large chunks - are allocated in their own untracked mmapped regions, separate from - others in this mspace. By default large chunks are not tracked, - which reduces fragmentation. However, such chunks are not - necessarily released to the system upon destroy_mspace. Enabling - tracking by setting to true may increase fragmentation, but avoids - leakage when relying on destroy_mspace to release all memory - allocated using this space. The function returns the previous - setting. -*/ -int mspace_track_large_chunks(mspace msp, int enable); - - -/* - mspace_malloc behaves as malloc, but operates within - the given space. -*/ -void* mspace_malloc(mspace msp, size_t bytes); - -/* - mspace_free behaves as free, but operates within - the given space. - - If compiled with FOOTERS==1, mspace_free is not actually needed. - free may be called instead of mspace_free because freed chunks from - any space are handled by their originating spaces. -*/ -void mspace_free(mspace msp, void* mem); - -/* - mspace_realloc behaves as realloc, but operates within - the given space. - - If compiled with FOOTERS==1, mspace_realloc is not actually - needed. realloc may be called instead of mspace_realloc because - realloced chunks from any space are handled by their originating - spaces. -*/ -void* mspace_realloc(mspace msp, void* mem, size_t newsize); - -/* - mspace_calloc behaves as calloc, but operates within - the given space. -*/ -void* mspace_calloc(mspace msp, size_t n_elements, size_t elem_size); - -/* - mspace_memalign behaves as memalign, but operates within - the given space. -*/ -void* mspace_memalign(mspace msp, size_t alignment, size_t bytes); - -/* - mspace_independent_calloc behaves as independent_calloc, but - operates within the given space. -*/ -void** mspace_independent_calloc(mspace msp, size_t n_elements, - size_t elem_size, void* chunks[]); - -/* - mspace_independent_comalloc behaves as independent_comalloc, but - operates within the given space. -*/ -void** mspace_independent_comalloc(mspace msp, size_t n_elements, - size_t sizes[], void* chunks[]); - -/* - mspace_footprint() returns the number of bytes obtained from the - system for this space. -*/ -size_t mspace_footprint(mspace msp); - -/* - mspace_max_footprint() returns the peak number of bytes obtained from the - system for this space. -*/ -size_t mspace_max_footprint(mspace msp); - - + + /* + mspace is an opaque type representing an independent + region of space that supports mspace_malloc, etc. + */ + typedef void* mspace; + + /* + create_mspace creates and returns a new independent space with the + given initial capacity, or, if 0, the default granularity size. It + returns null if there is no system memory available to create the + space. If argument locked is non-zero, the space uses a separate + lock to control access. The capacity of the space will grow + dynamically as needed to service mspace_malloc requests. You can + control the sizes of incremental increases of this space by + compiling with a different DEFAULT_GRANULARITY or dynamically + setting with mallopt(M_GRANULARITY, value). + */ + DLMALLOC_EXPORT mspace create_mspace(size_t capacity, int locked); + + /* + destroy_mspace destroys the given space, and attempts to return all + of its memory back to the system, returning the total number of + bytes freed. After destruction, the results of access to all memory + used by the space become undefined. + */ + DLMALLOC_EXPORT size_t destroy_mspace(mspace msp); + + /* + create_mspace_with_base uses the memory supplied as the initial base + of a new mspace. Part (less than 128*sizeof(size_t) bytes) of this + space is used for bookkeeping, so the capacity must be at least this + large. (Otherwise 0 is returned.) When this initial space is + exhausted, additional memory will be obtained from the system. + Destroying this space will deallocate all additionally allocated + space (if possible) but not the initial base. + */ + DLMALLOC_EXPORT mspace create_mspace_with_base(void* base, size_t capacity, int locked); + + /* + mspace_track_large_chunks controls whether requests for large chunks + are allocated in their own untracked mmapped regions, separate from + others in this mspace. By default large chunks are not tracked, + which reduces fragmentation. However, such chunks are not + necessarily released to the system upon destroy_mspace. Enabling + tracking by setting to true may increase fragmentation, but avoids + leakage when relying on destroy_mspace to release all memory + allocated using this space. The function returns the previous + setting. + */ + DLMALLOC_EXPORT int mspace_track_large_chunks(mspace msp, int enable); + + + /* + mspace_malloc behaves as malloc, but operates within + the given space. + */ + DLMALLOC_EXPORT void* mspace_malloc(mspace msp, size_t bytes); + + /* + mspace_free behaves as free, but operates within + the given space. + + If compiled with FOOTERS==1, mspace_free is not actually needed. + free may be called instead of mspace_free because freed chunks from + any space are handled by their originating spaces. + */ + DLMALLOC_EXPORT void mspace_free(mspace msp, void* mem); + + /* + mspace_realloc behaves as realloc, but operates within + the given space. + + If compiled with FOOTERS==1, mspace_realloc is not actually + needed. realloc may be called instead of mspace_realloc because + realloced chunks from any space are handled by their originating + spaces. + */ + DLMALLOC_EXPORT void* mspace_realloc(mspace msp, void* mem, size_t newsize); + + /* + mspace_calloc behaves as calloc, but operates within + the given space. + */ + DLMALLOC_EXPORT void* mspace_calloc(mspace msp, size_t n_elements, size_t elem_size); + + /* + mspace_memalign behaves as memalign, but operates within + the given space. + */ + DLMALLOC_EXPORT void* mspace_memalign(mspace msp, size_t alignment, size_t bytes); + + /* + mspace_independent_calloc behaves as independent_calloc, but + operates within the given space. + */ + DLMALLOC_EXPORT void** mspace_independent_calloc(mspace msp, size_t n_elements, + size_t elem_size, void* chunks[]); + + /* + mspace_independent_comalloc behaves as independent_comalloc, but + operates within the given space. + */ + DLMALLOC_EXPORT void** mspace_independent_comalloc(mspace msp, size_t n_elements, + size_t sizes[], void* chunks[]); + + /* + mspace_footprint() returns the number of bytes obtained from the + system for this space. + */ + DLMALLOC_EXPORT size_t mspace_footprint(mspace msp); + + /* + mspace_max_footprint() returns the peak number of bytes obtained from the + system for this space. + */ + DLMALLOC_EXPORT size_t mspace_max_footprint(mspace msp); + + #if !NO_MALLINFO -/* - mspace_mallinfo behaves as mallinfo, but reports properties of - the given space. -*/ -struct mallinfo mspace_mallinfo(mspace msp); + /* + mspace_mallinfo behaves as mallinfo, but reports properties of + the given space. + */ + DLMALLOC_EXPORT struct mallinfo mspace_mallinfo(mspace msp); #endif /* NO_MALLINFO */ - -/* - malloc_usable_size(void* p) behaves the same as malloc_usable_size; -*/ - size_t mspace_usable_size(void* mem); - -/* - mspace_malloc_stats behaves as malloc_stats, but reports - properties of the given space. -*/ -void mspace_malloc_stats(mspace msp); - -/* - mspace_trim behaves as malloc_trim, but - operates within the given space. -*/ -int mspace_trim(mspace msp, size_t pad); - -/* - An alias for mallopt. -*/ -int mspace_mallopt(int, int); - + + /* + malloc_usable_size(void* p) behaves the same as malloc_usable_size; + */ + DLMALLOC_EXPORT size_t mspace_usable_size(const void* mem); + + /* + mspace_malloc_stats behaves as malloc_stats, but reports + properties of the given space. + */ + DLMALLOC_EXPORT void mspace_malloc_stats(mspace msp); + + /* + mspace_trim behaves as malloc_trim, but + operates within the given space. + */ + DLMALLOC_EXPORT int mspace_trim(mspace msp, size_t pad); + + /* + An alias for mallopt. + */ + DLMALLOC_EXPORT int mspace_mallopt(int, int); + #endif /* MSPACES */ - + #ifdef __cplusplus -}; /* end of extern "C" */ +} /* end of extern "C" */ #endif /* __cplusplus */ /* - ======================================================================== - To make a fully customizable malloc.h header file, cut everything - above this line, put into file malloc.h, edit to suit, and #include it - on the next line, as well as in programs that use this malloc. - ======================================================================== -*/ + ======================================================================== + To make a fully customizable malloc.h header file, cut everything + above this line, put into file malloc.h, edit to suit, and #include it + on the next line, as well as in programs that use this malloc. + ======================================================================== + */ /* #include "malloc.h" */ /*------------------------------ internal #includes ---------------------- */ -#ifdef WIN32 +#ifdef _MSC_VER #pragma warning( disable : 4146 ) /* no "unsigned" warnings */ -#endif /* WIN32 */ - +#endif /* _MSC_VER */ +#if !NO_MALLOC_STATS #include <stdio.h> /* for printing in malloc_stats */ -#include <time.h> /* for magic initialization */ - +#endif /* NO_MALLOC_STATS */ #ifndef LACKS_ERRNO_H #include <errno.h> /* for MALLOC_FAILURE_ACTION */ #endif /* LACKS_ERRNO_H */ -#if FOOTERS || DEBUG -#include <time.h> /* for magic initialization */ -#endif /* FOOTERS */ -#ifndef LACKS_STDLIB_H -#include <stdlib.h> /* for abort() */ -#endif /* LACKS_STDLIB_H */ #ifdef DEBUG #if ABORT_ON_ASSERT_FAILURE #undef assert @@ -1315,6 +1465,12 @@ int mspace_mallopt(int, int); #endif #define DEBUG 0 #endif /* DEBUG */ +#if !defined(WIN32) && !defined(LACKS_TIME_H) +#include <time.h> /* for magic initialization */ +#endif /* WIN32 */ +#ifndef LACKS_STDLIB_H +#include <stdlib.h> /* for abort() */ +#endif /* LACKS_STDLIB_H */ #ifndef LACKS_STRING_H #include <string.h> /* for memset etc */ #endif /* LACKS_STRING_H */ @@ -1349,18 +1505,22 @@ extern void* sbrk(ptrdiff_t); /* Declarations for locking */ #if USE_LOCKS #ifndef WIN32 -#include <pthread.h> #if defined (__SVR4) && defined (__sun) /* solaris */ #include <thread.h> -#endif /* solaris */ -#else +#elif !defined(LACKS_SCHED_H) +#include <sched.h> +#endif /* solaris or LACKS_SCHED_H */ +#if (defined(USE_RECURSIVE_LOCKS) && USE_RECURSIVE_LOCKS != 0) || !USE_SPIN_LOCKS +#include <pthread.h> +#endif /* USE_RECURSIVE_LOCKS ... */ +#elif defined(_MSC_VER) #ifndef _M_AMD64 /* These are already defined on AMD64 builds */ #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ -LONG __cdecl _InterlockedCompareExchange(LONG volatile *Dest, LONG Exchange, LONG Comp); -LONG __cdecl _InterlockedExchange(LONG volatile *Target, LONG Value); + LONG __cdecl _InterlockedCompareExchange(LONG volatile *Dest, LONG Exchange, LONG Comp); + LONG __cdecl _InterlockedExchange(LONG volatile *Target, LONG Value); #ifdef __cplusplus } #endif /* __cplusplus */ @@ -1369,17 +1529,25 @@ LONG __cdecl _InterlockedExchange(LONG volatile *Target, LONG Value); #pragma intrinsic (_InterlockedExchange) #define interlockedcompareexchange _InterlockedCompareExchange #define interlockedexchange _InterlockedExchange +#elif defined(WIN32) && defined(__GNUC__) +#define interlockedcompareexchange(a, b, c) __sync_val_compare_and_swap(a, c, b) +#define interlockedexchange __sync_lock_test_and_set #endif /* Win32 */ +#else /* USE_LOCKS */ #endif /* USE_LOCKS */ +#ifndef LOCK_AT_FORK +#define LOCK_AT_FORK 0 +#endif + /* Declarations for bit scanning on win32 */ #if defined(_MSC_VER) && _MSC_VER>=1300 -#ifndef BitScanForward /* Try to avoid pulling in WinNT.h */ +#ifndef BitScanForward /* Try to avoid pulling in WinNT.h */ #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ -unsigned char _BitScanForward(unsigned long *index, unsigned long mask); -unsigned char _BitScanReverse(unsigned long *index, unsigned long mask); + unsigned char _BitScanForward(unsigned long *index, unsigned long mask); + unsigned char _BitScanReverse(unsigned long *index, unsigned long mask); #ifdef __cplusplus } #endif /* __cplusplus */ @@ -1402,7 +1570,7 @@ unsigned char _BitScanReverse(unsigned long *index, unsigned long mask); # define malloc_getpagesize sysconf(_SC_PAGE_SIZE) # else # if defined(BSD) || defined(DGUX) || defined(HAVE_GETPAGESIZE) - extern size_t getpagesize(); +extern size_t getpagesize(); # define malloc_getpagesize getpagesize() # else # ifdef WIN32 /* use supplied emulation of getpagesize */ @@ -1438,8 +1606,6 @@ unsigned char _BitScanReverse(unsigned long *index, unsigned long mask); #endif #endif - - /* ------------------- size_t and alignment properties -------------------- */ /* The byte and bit size of a size_t */ @@ -1465,16 +1631,16 @@ unsigned char _BitScanReverse(unsigned long *index, unsigned long mask); /* the number of bytes to offset an address to align it */ #define align_offset(A)\ - ((((size_t)(A) & CHUNK_ALIGN_MASK) == 0)? 0 :\ - ((MALLOC_ALIGNMENT - ((size_t)(A) & CHUNK_ALIGN_MASK)) & CHUNK_ALIGN_MASK)) +((((size_t)(A) & CHUNK_ALIGN_MASK) == 0)? 0 :\ +((MALLOC_ALIGNMENT - ((size_t)(A) & CHUNK_ALIGN_MASK)) & CHUNK_ALIGN_MASK)) /* -------------------------- MMAP preliminaries ------------------------- */ /* - If HAVE_MORECORE or HAVE_MMAP are false, we just define calls and - checks to fail so compiler optimizer can delete code rather than - using so many "#if"s. -*/ + If HAVE_MORECORE or HAVE_MMAP are false, we just define calls and + checks to fail so compiler optimizer can delete code rather than + using so many "#if"s. + */ /* MORECORE and MMAP must return MFAIL on failure */ @@ -1494,15 +1660,15 @@ unsigned char _BitScanReverse(unsigned long *index, unsigned long mask); #define MMAP_DEFAULT(s) mmap(0, (s), MMAP_PROT, MMAP_FLAGS, -1, 0) #else /* MAP_ANONYMOUS */ /* - Nearly all versions of mmap support MAP_ANONYMOUS, so the following - is unlikely to be needed, but is supplied just in case. -*/ + Nearly all versions of mmap support MAP_ANONYMOUS, so the following + is unlikely to be needed, but is supplied just in case. + */ #define MMAP_FLAGS (MAP_PRIVATE) static int dev_zero_fd = -1; /* Cached file descriptor for /dev/zero. */ #define MMAP_DEFAULT(s) ((dev_zero_fd < 0) ? \ - (dev_zero_fd = open("/dev/zero", O_RDWR), \ - mmap(0, (s), MMAP_PROT, MMAP_FLAGS, dev_zero_fd, 0)) : \ - mmap(0, (s), MMAP_PROT, MMAP_FLAGS, dev_zero_fd, 0)) +(dev_zero_fd = open("/dev/zero", O_RDWR), \ +mmap(0, (s), MMAP_PROT, MMAP_FLAGS, dev_zero_fd, 0)) : \ +mmap(0, (s), MMAP_PROT, MMAP_FLAGS, dev_zero_fd, 0)) #endif /* MAP_ANONYMOUS */ #define DIRECT_MMAP_DEFAULT(s) MMAP_DEFAULT(s) @@ -1511,33 +1677,33 @@ static int dev_zero_fd = -1; /* Cached file descriptor for /dev/zero. */ /* Win32 MMAP via VirtualAlloc */ static FORCEINLINE void* win32mmap(size_t size) { - void* ptr = VirtualAlloc(0, size, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE); - return (ptr != 0)? ptr: MFAIL; + void* ptr = VirtualAlloc(0, size, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE); + return (ptr != 0)? ptr: MFAIL; } /* For direct MMAP, use MEM_TOP_DOWN to minimize interference */ static FORCEINLINE void* win32direct_mmap(size_t size) { - void* ptr = VirtualAlloc(0, size, MEM_RESERVE|MEM_COMMIT|MEM_TOP_DOWN, - PAGE_READWRITE); - return (ptr != 0)? ptr: MFAIL; + void* ptr = VirtualAlloc(0, size, MEM_RESERVE|MEM_COMMIT|MEM_TOP_DOWN, + PAGE_READWRITE); + return (ptr != 0)? ptr: MFAIL; } /* This function supports releasing coalesed segments */ static FORCEINLINE int win32munmap(void* ptr, size_t size) { - MEMORY_BASIC_INFORMATION minfo; - char* cptr = (char*)ptr; - while (size) { - if (VirtualQuery(cptr, &minfo, sizeof(minfo)) == 0) - return -1; - if (minfo.BaseAddress != cptr || minfo.AllocationBase != cptr || - minfo.State != MEM_COMMIT || minfo.RegionSize > size) - return -1; - if (VirtualFree(cptr, 0, MEM_RELEASE) == 0) - return -1; - cptr += minfo.RegionSize; - size -= minfo.RegionSize; - } - return 0; + MEMORY_BASIC_INFORMATION minfo; + char* cptr = (char*)ptr; + while (size) { + if (VirtualQuery(cptr, &minfo, sizeof(minfo)) == 0) + return -1; + if (minfo.BaseAddress != cptr || minfo.AllocationBase != cptr || + minfo.State != MEM_COMMIT || minfo.RegionSize > size) + return -1; + if (VirtualFree(cptr, 0, MEM_RELEASE) == 0) + return -1; + cptr += minfo.RegionSize; + size -= minfo.RegionSize; + } + return 0; } #define MMAP_DEFAULT(s) win32mmap(s) @@ -1552,63 +1718,62 @@ static FORCEINLINE int win32munmap(void* ptr, size_t size) { #endif /* WIN32 */ #endif /* HAVE_MREMAP */ - /** * Define CALL_MORECORE */ #if HAVE_MORECORE - #ifdef MORECORE - #define CALL_MORECORE(S) MORECORE(S) - #else /* MORECORE */ - #define CALL_MORECORE(S) MORECORE_DEFAULT(S) - #endif /* MORECORE */ +#ifdef MORECORE +#define CALL_MORECORE(S) MORECORE(S) +#else /* MORECORE */ +#define CALL_MORECORE(S) MORECORE_DEFAULT(S) +#endif /* MORECORE */ #else /* HAVE_MORECORE */ - #define CALL_MORECORE(S) MFAIL +#define CALL_MORECORE(S) MFAIL #endif /* HAVE_MORECORE */ /** * Define CALL_MMAP/CALL_MUNMAP/CALL_DIRECT_MMAP */ #if HAVE_MMAP - #define USE_MMAP_BIT (SIZE_T_ONE) - - #ifdef MMAP - #define CALL_MMAP(s) MMAP(s) - #else /* MMAP */ - #define CALL_MMAP(s) MMAP_DEFAULT(s) - #endif /* MMAP */ - #ifdef MUNMAP - #define CALL_MUNMAP(a, s) MUNMAP((a), (s)) - #else /* MUNMAP */ - #define CALL_MUNMAP(a, s) MUNMAP_DEFAULT((a), (s)) - #endif /* MUNMAP */ - #ifdef DIRECT_MMAP - #define CALL_DIRECT_MMAP(s) DIRECT_MMAP(s) - #else /* DIRECT_MMAP */ - #define CALL_DIRECT_MMAP(s) DIRECT_MMAP_DEFAULT(s) - #endif /* DIRECT_MMAP */ +#define USE_MMAP_BIT (SIZE_T_ONE) + +#ifdef MMAP +#define CALL_MMAP(s) MMAP(s) +#else /* MMAP */ +#define CALL_MMAP(s) MMAP_DEFAULT(s) +#endif /* MMAP */ +#ifdef MUNMAP +#define CALL_MUNMAP(a, s) MUNMAP((a), (s)) +#else /* MUNMAP */ +#define CALL_MUNMAP(a, s) MUNMAP_DEFAULT((a), (s)) +#endif /* MUNMAP */ +#ifdef DIRECT_MMAP +#define CALL_DIRECT_MMAP(s) DIRECT_MMAP(s) +#else /* DIRECT_MMAP */ +#define CALL_DIRECT_MMAP(s) DIRECT_MMAP_DEFAULT(s) +#endif /* DIRECT_MMAP */ #else /* HAVE_MMAP */ - #define USE_MMAP_BIT (SIZE_T_ZERO) - - #define MMAP(s) MFAIL - #define MUNMAP(a, s) (-1) - #define DIRECT_MMAP(s) MFAIL - #define CALL_DIRECT_MMAP(s) DIRECT_MMAP(s) - #define CALL_MMAP(s) MMAP(s) - #define CALL_MUNMAP(a, s) MUNMAP((a), (s)) +#define USE_MMAP_BIT (SIZE_T_ZERO) + +#define MMAP(s) MFAIL +#define MUNMAP(a, s) (-1) +#define DIRECT_MMAP(s) MFAIL +#define CALL_DIRECT_MMAP(s) DIRECT_MMAP(s) +#define CALL_MMAP(s) MMAP(s) +#define CALL_MUNMAP(a, s) MUNMAP((a), (s)) #endif /* HAVE_MMAP */ /** * Define CALL_MREMAP */ #if HAVE_MMAP && HAVE_MREMAP - #ifdef MREMAP - #define CALL_MREMAP(addr, osz, nsz, mv) MREMAP((addr), (osz), (nsz), (mv)) - #else /* MREMAP */ - #define CALL_MREMAP(addr, osz, nsz, mv) MREMAP_DEFAULT((addr), (osz), (nsz), (mv)) - #endif /* MREMAP */ +#ifdef MREMAP +#define CALL_MREMAP(addr, osz, nsz, mv) MREMAP((addr), (osz), (nsz), (mv)) +#else /* MREMAP */ +#define CALL_MREMAP(addr, osz, nsz, mv) MREMAP_DEFAULT((addr), (osz), (nsz), (mv)) +#endif /* MREMAP */ #else /* HAVE_MMAP && HAVE_MREMAP */ - #define CALL_MREMAP(addr, osz, nsz, mv) MFAIL +#define CALL_MREMAP(addr, osz, nsz, mv) MFAIL #endif /* HAVE_MMAP && HAVE_MREMAP */ /* mstate bit set if continguous morecore disabled or failed */ @@ -1621,449 +1786,418 @@ static FORCEINLINE int win32munmap(void* ptr, size_t size) { /* --------------------------- Lock preliminaries ------------------------ */ /* - When locks are defined, there is one global lock, plus - one per-mspace lock. - - The global lock_ensures that mparams.magic and other unique - mparams values are initialized only once. It also protects - sequences of calls to MORECORE. In many cases sys_alloc requires - two calls, that should not be interleaved with calls by other - threads. This does not protect against direct calls to MORECORE - by other threads not using this lock, so there is still code to - cope the best we can on interference. - - Per-mspace locks surround calls to malloc, free, etc. To enable use - in layered extensions, per-mspace locks are reentrant. - - Because lock-protected regions generally have bounded times, it is - OK to use the supplied simple spinlocks in the custom versions for - x86. Spinlocks are likely to improve performance for lightly - contended applications, but worsen performance under heavy - contention. - - If USE_LOCKS is > 1, the definitions of lock routines here are - bypassed, in which case you will need to define the type MLOCK_T, - and at least INITIAL_LOCK, ACQUIRE_LOCK, RELEASE_LOCK and possibly - TRY_LOCK (which is not used in this malloc, but commonly needed in - extensions.) You must also declare a - static MLOCK_T malloc_global_mutex = { initialization values };. - -*/ - -#if USE_LOCKS == 1 - -#if USE_SPIN_LOCKS && SPIN_LOCKS_AVAILABLE -#ifndef WIN32 - -/* Custom pthread-style spin locks on x86 and x64 for gcc */ -struct pthread_mlock_t { - volatile unsigned int l; - unsigned int c; - pthread_t threadid; -}; -#define MLOCK_T struct pthread_mlock_t -#define CURRENT_THREAD pthread_self() -#define INITIAL_LOCK(sl) ((sl)->threadid = 0, (sl)->l = (sl)->c = 0, 0) -#define ACQUIRE_LOCK(sl) pthread_acquire_lock(sl) -#define RELEASE_LOCK(sl) pthread_release_lock(sl) -#define TRY_LOCK(sl) pthread_try_lock(sl) -#define SPINS_PER_YIELD 63 + When locks are defined, there is one global lock, plus + one per-mspace lock. + + The global lock_ensures that mparams.magic and other unique + mparams values are initialized only once. It also protects + sequences of calls to MORECORE. In many cases sys_alloc requires + two calls, that should not be interleaved with calls by other + threads. This does not protect against direct calls to MORECORE + by other threads not using this lock, so there is still code to + cope the best we can on interference. + + Per-mspace locks surround calls to malloc, free, etc. + By default, locks are simple non-reentrant mutexes. + + Because lock-protected regions generally have bounded times, it is + OK to use the supplied simple spinlocks. Spinlocks are likely to + improve performance for lightly contended applications, but worsen + performance under heavy contention. + + If USE_LOCKS is > 1, the definitions of lock routines here are + bypassed, in which case you will need to define the type MLOCK_T, + and at least INITIAL_LOCK, DESTROY_LOCK, ACQUIRE_LOCK, RELEASE_LOCK + and TRY_LOCK. You must also declare a + static MLOCK_T malloc_global_mutex = { initialization values };. + + */ -static MLOCK_T malloc_global_mutex = { 0, 0, 0}; +#if !USE_LOCKS +#define USE_LOCK_BIT (0U) +#define INITIAL_LOCK(l) (0) +#define DESTROY_LOCK(l) (0) +#define ACQUIRE_MALLOC_GLOBAL_LOCK() +#define RELEASE_MALLOC_GLOBAL_LOCK() -static FORCEINLINE int pthread_acquire_lock (MLOCK_T *sl) { - int spins = 0; - volatile unsigned int* lp = &sl->l; - for (;;) { - if (*lp != 0) { - if (sl->threadid == CURRENT_THREAD) { - ++sl->c; - return 0; - } - } - else { - /* place args to cmpxchgl in locals to evade oddities in some gccs */ - int cmp = 0; - int val = 1; - int ret; - __asm__ __volatile__ ("lock; cmpxchgl %1, %2" - : "=a" (ret) - : "r" (val), "m" (*(lp)), "0"(cmp) - : "memory", "cc"); - if (!ret) { - assert(!sl->threadid); - sl->threadid = CURRENT_THREAD; - sl->c = 1; - return 0; - } - } - if ((++spins & SPINS_PER_YIELD) == 0) { -#if defined (__SVR4) && defined (__sun) /* solaris */ - thr_yield(); #else -#if defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__) - sched_yield(); -#else /* no-op yield on unknown systems */ - ; -#endif /* __linux__ || __FreeBSD__ || __APPLE__ */ -#endif /* solaris */ - } - } +#if USE_LOCKS > 1 +/* ----------------------- User-defined locks ------------------------ */ +/* Define your own lock implementation here */ +/* #define INITIAL_LOCK(lk) ... */ +/* #define DESTROY_LOCK(lk) ... */ +/* #define ACQUIRE_LOCK(lk) ... */ +/* #define RELEASE_LOCK(lk) ... */ +/* #define TRY_LOCK(lk) ... */ +/* static MLOCK_T malloc_global_mutex = ... */ + +#elif USE_SPIN_LOCKS + +/* First, define CAS_LOCK and CLEAR_LOCK on ints */ +/* Note CAS_LOCK defined to return 0 on success */ + +#if defined(__GNUC__)&& (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)) +#define CAS_LOCK(sl) __sync_lock_test_and_set(sl, 1) +#define CLEAR_LOCK(sl) __sync_lock_release(sl) + +#elif (defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))) +/* Custom spin locks for older gcc on x86 */ +static FORCEINLINE int x86_cas_lock(int *sl) { + int ret; + int val = 1; + int cmp = 0; + __asm__ __volatile__ ("lock; cmpxchgl %1, %2" + : "=a" (ret) + : "r" (val), "m" (*(sl)), "0"(cmp) + : "memory", "cc"); + return ret; } -static FORCEINLINE void pthread_release_lock (MLOCK_T *sl) { - volatile unsigned int* lp = &sl->l; - assert(*lp != 0); - assert(sl->threadid == CURRENT_THREAD); - if (--sl->c == 0) { - sl->threadid = 0; +static FORCEINLINE void x86_clear_lock(int* sl) { + assert(*sl != 0); int prev = 0; int ret; __asm__ __volatile__ ("lock; xchgl %0, %1" : "=r" (ret) - : "m" (*(lp)), "0"(prev) + : "m" (*(sl)), "0"(prev) : "memory"); - } } -static FORCEINLINE int pthread_try_lock (MLOCK_T *sl) { - volatile unsigned int* lp = &sl->l; - if (*lp != 0) { - if (sl->threadid == CURRENT_THREAD) { - ++sl->c; - return 1; - } - } - else { - int cmp = 0; - int val = 1; - int ret; - __asm__ __volatile__ ("lock; cmpxchgl %1, %2" - : "=a" (ret) - : "r" (val), "m" (*(lp)), "0"(cmp) - : "memory", "cc"); - if (!ret) { - assert(!sl->threadid); - sl->threadid = CURRENT_THREAD; - sl->c = 1; - return 1; +#define CAS_LOCK(sl) x86_cas_lock(sl) +#define CLEAR_LOCK(sl) x86_clear_lock(sl) + +#else /* Win32 MSC */ +#define CAS_LOCK(sl) interlockedexchange(sl, (LONG)1) +#define CLEAR_LOCK(sl) interlockedexchange (sl, (LONG)0) + +#endif /* ... gcc spins locks ... */ + +/* How to yield for a spin lock */ +#define SPINS_PER_YIELD 63 +#if defined(_MSC_VER) +#define SLEEP_EX_DURATION 50 /* delay for yield/sleep */ +#define SPIN_LOCK_YIELD SleepEx(SLEEP_EX_DURATION, FALSE) +#elif defined (__SVR4) && defined (__sun) /* solaris */ +#define SPIN_LOCK_YIELD thr_yield(); +#elif !defined(LACKS_SCHED_H) +#define SPIN_LOCK_YIELD sched_yield(); +#else +#define SPIN_LOCK_YIELD +#endif /* ... yield ... */ + +#if !defined(USE_RECURSIVE_LOCKS) || USE_RECURSIVE_LOCKS == 0 +/* Plain spin locks use single word (embedded in malloc_states) */ +static int spin_acquire_lock(int *sl) { + int spins = 0; + while (*(volatile int *)sl != 0 || CAS_LOCK(sl)) { + if ((++spins & SPINS_PER_YIELD) == 0) { + SPIN_LOCK_YIELD; + } } - } - return 0; + return 0; } +#define MLOCK_T int +#define TRY_LOCK(sl) !CAS_LOCK(sl) +#define RELEASE_LOCK(sl) CLEAR_LOCK(sl) +#define ACQUIRE_LOCK(sl) (CAS_LOCK(sl)? spin_acquire_lock(sl) : 0) +#define INITIAL_LOCK(sl) (*sl = 0) +#define DESTROY_LOCK(sl) (0) +static MLOCK_T malloc_global_mutex = 0; -#else /* WIN32 */ -/* Custom win32-style spin locks on x86 and x64 for MSC */ -struct win32_mlock_t { - volatile long l; - unsigned int c; - long threadid; -}; - -#define MLOCK_T struct win32_mlock_t +#else /* USE_RECURSIVE_LOCKS */ +/* types for lock owners */ +#ifdef WIN32 +#define THREAD_ID_T DWORD #define CURRENT_THREAD GetCurrentThreadId() -#define INITIAL_LOCK(sl) ((sl)->threadid = 0, (sl)->l = (sl)->c = 0, 0) -#define ACQUIRE_LOCK(sl) win32_acquire_lock(sl) -#define RELEASE_LOCK(sl) win32_release_lock(sl) -#define TRY_LOCK(sl) win32_try_lock(sl) -#define SPINS_PER_YIELD 63 +#define EQ_OWNER(X,Y) ((X) == (Y)) +#else +/* + Note: the following assume that pthread_t is a type that can be + initialized to (casted) zero. If this is not the case, you will need to + somehow redefine these or not use spin locks. + */ +#define THREAD_ID_T pthread_t +#define CURRENT_THREAD pthread_self() +#define EQ_OWNER(X,Y) pthread_equal(X, Y) +#endif -static MLOCK_T malloc_global_mutex = { 0, 0, 0}; +struct malloc_recursive_lock { + int sl; + unsigned int c; + THREAD_ID_T threadid; +}; -static FORCEINLINE int win32_acquire_lock (MLOCK_T *sl) { - int spins = 0; - for (;;) { - if (sl->l != 0) { - if (sl->threadid == CURRENT_THREAD) { - ++sl->c; - return 0; - } - } - else { - if (!interlockedexchange(&sl->l, 1)) { - assert(!sl->threadid); - sl->threadid = CURRENT_THREAD; - sl->c = 1; - return 0; - } +#define MLOCK_T struct malloc_recursive_lock +static MLOCK_T malloc_global_mutex = { 0, 0, (THREAD_ID_T)0}; + +static FORCEINLINE void recursive_release_lock(MLOCK_T *lk) { + assert(lk->sl != 0); + if (--lk->c == 0) { + CLEAR_LOCK(&lk->sl); } - if ((++spins & SPINS_PER_YIELD) == 0) - SleepEx(0, FALSE); - } } -static FORCEINLINE void win32_release_lock (MLOCK_T *sl) { - assert(sl->threadid == CURRENT_THREAD); - assert(sl->l != 0); - if (--sl->c == 0) { - sl->threadid = 0; - interlockedexchange (&sl->l, 0); - } +static FORCEINLINE int recursive_acquire_lock(MLOCK_T *lk) { + THREAD_ID_T mythreadid = CURRENT_THREAD; + int spins = 0; + for (;;) { + if (*((volatile int *)(&lk->sl)) == 0) { + if (!CAS_LOCK(&lk->sl)) { + lk->threadid = mythreadid; + lk->c = 1; + return 0; + } + } + else if (EQ_OWNER(lk->threadid, mythreadid)) { + ++lk->c; + return 0; + } + if ((++spins & SPINS_PER_YIELD) == 0) { + SPIN_LOCK_YIELD; + } + } } -static FORCEINLINE int win32_try_lock (MLOCK_T *sl) { - if (sl->l != 0) { - if (sl->threadid == CURRENT_THREAD) { - ++sl->c; - return 1; +static FORCEINLINE int recursive_try_lock(MLOCK_T *lk) { + THREAD_ID_T mythreadid = CURRENT_THREAD; + if (*((volatile int *)(&lk->sl)) == 0) { + if (!CAS_LOCK(&lk->sl)) { + lk->threadid = mythreadid; + lk->c = 1; + return 1; + } } - } - else { - if (!interlockedexchange(&sl->l, 1)){ - assert(!sl->threadid); - sl->threadid = CURRENT_THREAD; - sl->c = 1; - return 1; + else if (EQ_OWNER(lk->threadid, mythreadid)) { + ++lk->c; + return 1; } - } - return 0; + return 0; } -#endif /* WIN32 */ -#else /* USE_SPIN_LOCKS */ - -#ifndef WIN32 -/* pthreads-based locks */ - -#define MLOCK_T pthread_mutex_t -#define CURRENT_THREAD pthread_self() -#define INITIAL_LOCK(sl) pthread_init_lock(sl) -#define ACQUIRE_LOCK(sl) pthread_mutex_lock(sl) -#define RELEASE_LOCK(sl) pthread_mutex_unlock(sl) -#define TRY_LOCK(sl) (!pthread_mutex_trylock(sl)) +#define RELEASE_LOCK(lk) recursive_release_lock(lk) +#define TRY_LOCK(lk) recursive_try_lock(lk) +#define ACQUIRE_LOCK(lk) recursive_acquire_lock(lk) +#define INITIAL_LOCK(lk) ((lk)->threadid = (THREAD_ID_T)0, (lk)->sl = 0, (lk)->c = 0) +#define DESTROY_LOCK(lk) (0) +#endif /* USE_RECURSIVE_LOCKS */ -static MLOCK_T malloc_global_mutex = PTHREAD_MUTEX_INITIALIZER; - -/* Cope with old-style linux recursive lock initialization by adding */ -/* skipped internal declaration from pthread.h */ -#ifdef linux -#ifndef PTHREAD_MUTEX_RECURSIVE -extern int pthread_mutexattr_setkind_np __P ((pthread_mutexattr_t *__attr, - int __kind)); -#define PTHREAD_MUTEX_RECURSIVE PTHREAD_MUTEX_RECURSIVE_NP -#define pthread_mutexattr_settype(x,y) pthread_mutexattr_setkind_np(x,y) -#endif -#endif - -static int pthread_init_lock (MLOCK_T *sl) { - pthread_mutexattr_t attr; - if (pthread_mutexattr_init(&attr)) return 1; - if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE)) return 1; - if (pthread_mutex_init(sl, &attr)) return 1; - if (pthread_mutexattr_destroy(&attr)) return 1; - return 0; -} - -#else /* WIN32 */ -/* Win32 critical sections */ +#elif defined(WIN32) /* Win32 critical sections */ #define MLOCK_T CRITICAL_SECTION -#define CURRENT_THREAD GetCurrentThreadId() -#define INITIAL_LOCK(s) (!InitializeCriticalSectionAndSpinCount((s), 0x80000000|4000)) -#define ACQUIRE_LOCK(s) (EnterCriticalSection(sl), 0) -#define RELEASE_LOCK(s) LeaveCriticalSection(sl) -#define TRY_LOCK(s) TryEnterCriticalSection(sl) +#define ACQUIRE_LOCK(lk) (EnterCriticalSection(lk), 0) +#define RELEASE_LOCK(lk) LeaveCriticalSection(lk) +#define TRY_LOCK(lk) TryEnterCriticalSection(lk) +#define INITIAL_LOCK(lk) (!InitializeCriticalSectionAndSpinCount((lk), 0x80000000|4000)) +#define DESTROY_LOCK(lk) (DeleteCriticalSection(lk), 0) #define NEED_GLOBAL_LOCK_INIT static MLOCK_T malloc_global_mutex; -static volatile long malloc_global_mutex_status; +static volatile LONG malloc_global_mutex_status; /* Use spin loop to initialize global lock */ static void init_malloc_global_mutex() { - for (;;) { - long stat = malloc_global_mutex_status; - if (stat > 0) - return; - /* transition to < 0 while initializing, then to > 0) */ - if (stat == 0 && - interlockedcompareexchange(&malloc_global_mutex_status, -1, 0) == 0) { - InitializeCriticalSection(&malloc_global_mutex); - interlockedexchange(&malloc_global_mutex_status,1); - return; + for (;;) { + long stat = malloc_global_mutex_status; + if (stat > 0) + return; + /* transition to < 0 while initializing, then to > 0) */ + if (stat == 0 && + interlockedcompareexchange(&malloc_global_mutex_status, (LONG)-1, (LONG)0) == 0) { + InitializeCriticalSection(&malloc_global_mutex); + interlockedexchange(&malloc_global_mutex_status, (LONG)1); + return; + } + SleepEx(0, FALSE); } - SleepEx(0, FALSE); - } } -#endif /* WIN32 */ -#endif /* USE_SPIN_LOCKS */ -#endif /* USE_LOCKS == 1 */ +#else /* pthreads-based locks */ +#define MLOCK_T pthread_mutex_t +#define ACQUIRE_LOCK(lk) pthread_mutex_lock(lk) +#define RELEASE_LOCK(lk) pthread_mutex_unlock(lk) +#define TRY_LOCK(lk) (!pthread_mutex_trylock(lk)) +#define INITIAL_LOCK(lk) pthread_init_lock(lk) +#define DESTROY_LOCK(lk) pthread_mutex_destroy(lk) -/* ----------------------- User-defined locks ------------------------ */ +#if defined(USE_RECURSIVE_LOCKS) && USE_RECURSIVE_LOCKS != 0 && defined(linux) && !defined(PTHREAD_MUTEX_RECURSIVE) +/* Cope with old-style linux recursive lock initialization by adding */ +/* skipped internal declaration from pthread.h */ +extern int pthread_mutexattr_setkind_np __P ((pthread_mutexattr_t *__attr, + int __kind)); +#define PTHREAD_MUTEX_RECURSIVE PTHREAD_MUTEX_RECURSIVE_NP +#define pthread_mutexattr_settype(x,y) pthread_mutexattr_setkind_np(x,y) +#endif /* USE_RECURSIVE_LOCKS ... */ -#if USE_LOCKS > 1 -/* Define your own lock implementation here */ -/* #define INITIAL_LOCK(sl) ... */ -/* #define ACQUIRE_LOCK(sl) ... */ -/* #define RELEASE_LOCK(sl) ... */ -/* #define TRY_LOCK(sl) ... */ -/* static MLOCK_T malloc_global_mutex = ... */ -#endif /* USE_LOCKS > 1 */ +static MLOCK_T malloc_global_mutex = PTHREAD_MUTEX_INITIALIZER; -/* ----------------------- Lock-based state ------------------------ */ +static int pthread_init_lock (MLOCK_T *lk) { + pthread_mutexattr_t attr; + if (pthread_mutexattr_init(&attr)) return 1; +#if defined(USE_RECURSIVE_LOCKS) && USE_RECURSIVE_LOCKS != 0 + if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE)) return 1; +#endif + if (pthread_mutex_init(lk, &attr)) return 1; + if (pthread_mutexattr_destroy(&attr)) return 1; + return 0; +} -#if USE_LOCKS +#endif /* ... lock types ... */ + +/* Common code for all lock types */ #define USE_LOCK_BIT (2U) -#else /* USE_LOCKS */ -#define USE_LOCK_BIT (0U) -#define INITIAL_LOCK(l) -#endif /* USE_LOCKS */ -#if USE_LOCKS #ifndef ACQUIRE_MALLOC_GLOBAL_LOCK #define ACQUIRE_MALLOC_GLOBAL_LOCK() ACQUIRE_LOCK(&malloc_global_mutex); #endif + #ifndef RELEASE_MALLOC_GLOBAL_LOCK #define RELEASE_MALLOC_GLOBAL_LOCK() RELEASE_LOCK(&malloc_global_mutex); #endif -#else /* USE_LOCKS */ -#define ACQUIRE_MALLOC_GLOBAL_LOCK() -#define RELEASE_MALLOC_GLOBAL_LOCK() -#endif /* USE_LOCKS */ +#endif /* USE_LOCKS */ /* ----------------------- Chunk representations ------------------------ */ /* - (The following includes lightly edited explanations by Colin Plumb.) - - The malloc_chunk declaration below is misleading (but accurate and - necessary). It declares a "view" into memory allowing access to - necessary fields at known offsets from a given base. - - Chunks of memory are maintained using a `boundary tag' method as - originally described by Knuth. (See the paper by Paul Wilson - ftp://ftp.cs.utexas.edu/pub/garbage/allocsrv.ps for a survey of such - techniques.) Sizes of free chunks are stored both in the front of - each chunk and at the end. This makes consolidating fragmented - chunks into bigger chunks fast. The head fields also hold bits - representing whether chunks are free or in use. - - Here are some pictures to make it clearer. They are "exploded" to - show that the state of a chunk can be thought of as extending from - the high 31 bits of the head field of its header through the - prev_foot and PINUSE_BIT bit of the following chunk header. - - A chunk that's in use looks like: - - chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - | Size of previous chunk (if P = 0) | - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |P| - | Size of this chunk 1| +-+ - mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - | | - +- -+ - | | - +- -+ - | : - +- size - sizeof(size_t) available payload bytes -+ - : | + (The following includes lightly edited explanations by Colin Plumb.) + + The malloc_chunk declaration below is misleading (but accurate and + necessary). It declares a "view" into memory allowing access to + necessary fields at known offsets from a given base. + + Chunks of memory are maintained using a `boundary tag' method as + originally described by Knuth. (See the paper by Paul Wilson + ftp://ftp.cs.utexas.edu/pub/garbage/allocsrv.ps for a survey of such + techniques.) Sizes of free chunks are stored both in the front of + each chunk and at the end. This makes consolidating fragmented + chunks into bigger chunks fast. The head fields also hold bits + representing whether chunks are free or in use. + + Here are some pictures to make it clearer. They are "exploded" to + show that the state of a chunk can be thought of as extending from + the high 31 bits of the head field of its header through the + prev_foot and PINUSE_BIT bit of the following chunk header. + + A chunk that's in use looks like: + + chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Size of previous chunk (if P = 0) | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |P| + | Size of this chunk 1| +-+ + mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | | + +- -+ + | | + +- -+ + | : + +- size - sizeof(size_t) available payload bytes -+ + : | chunk-> +- -+ - | | - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |1| - | Size of next chunk (may or may not be in use) | +-+ + | | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |1| + | Size of next chunk (may or may not be in use) | +-+ mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - - And if it's free, it looks like this: - - chunk-> +- -+ - | User payload (must be in use, or we would have merged!) | - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |P| - | Size of this chunk 0| +-+ - mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - | Next pointer | - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - | Prev pointer | - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - | : - +- size - sizeof(struct chunk) unused bytes -+ - : | + + And if it's free, it looks like this: + + chunk-> +- -+ + | User payload (must be in use, or we would have merged!) | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |P| + | Size of this chunk 0| +-+ + mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Next pointer | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Prev pointer | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | : + +- size - sizeof(struct chunk) unused bytes -+ + : | chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - | Size of this chunk | - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |0| - | Size of next chunk (must be in use, or we would have merged)| +-+ + | Size of this chunk | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |0| + | Size of next chunk (must be in use, or we would have merged)| +-+ mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - | : - +- User payload -+ - : | - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - |0| - +-+ - Note that since we always merge adjacent free chunks, the chunks - adjacent to a free chunk must be in use. - - Given a pointer to a chunk (which can be derived trivially from the - payload pointer) we can, in O(1) time, find out whether the adjacent - chunks are free, and if so, unlink them from the lists that they - are on and merge them with the current chunk. - - Chunks always begin on even word boundaries, so the mem portion - (which is returned to the user) is also on an even word boundary, and - thus at least double-word aligned. - - The P (PINUSE_BIT) bit, stored in the unused low-order bit of the - chunk size (which is always a multiple of two words), is an in-use - bit for the *previous* chunk. If that bit is *clear*, then the - word before the current chunk size contains the previous chunk - size, and can be used to find the front of the previous chunk. - The very first chunk allocated always has this bit set, preventing - access to non-existent (or non-owned) memory. If pinuse is set for - any given chunk, then you CANNOT determine the size of the - previous chunk, and might even get a memory addressing fault when - trying to do so. - - The C (CINUSE_BIT) bit, stored in the unused second-lowest bit of - the chunk size redundantly records whether the current chunk is - inuse (unless the chunk is mmapped). This redundancy enables usage - checks within free and realloc, and reduces indirection when freeing - and consolidating chunks. - - Each freshly allocated chunk must have both cinuse and pinuse set. - That is, each allocated chunk borders either a previously allocated - and still in-use chunk, or the base of its memory arena. This is - ensured by making all allocations from the the `lowest' part of any - found chunk. Further, no free chunk physically borders another one, - so each free chunk is known to be preceded and followed by either - inuse chunks or the ends of memory. - - Note that the `foot' of the current chunk is actually represented - as the prev_foot of the NEXT chunk. This makes it easier to - deal with alignments etc but can be very confusing when trying - to extend or adapt this code. - - The exceptions to all this are - - 1. The special chunk `top' is the top-most available chunk (i.e., - the one bordering the end of available memory). It is treated - specially. Top is never included in any bin, is used only if - no other chunk is available, and is released back to the - system if it is very large (see M_TRIM_THRESHOLD). In effect, - the top chunk is treated as larger (and thus less well - fitting) than any other available chunk. The top chunk - doesn't update its trailing size field since there is no next - contiguous chunk that would have to index off it. However, - space is still allocated for it (TOP_FOOT_SIZE) to enable - separation or merging when space is extended. - - 3. Chunks allocated via mmap, have both cinuse and pinuse bits - cleared in their head fields. Because they are allocated - one-by-one, each must carry its own prev_foot field, which is - also used to hold the offset this chunk has within its mmapped - region, which is needed to preserve alignment. Each mmapped - chunk is trailed by the first two fields of a fake next-chunk - for sake of usage checks. - -*/ + | : + +- User payload -+ + : | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + |0| + +-+ + Note that since we always merge adjacent free chunks, the chunks + adjacent to a free chunk must be in use. + + Given a pointer to a chunk (which can be derived trivially from the + payload pointer) we can, in O(1) time, find out whether the adjacent + chunks are free, and if so, unlink them from the lists that they + are on and merge them with the current chunk. + + Chunks always begin on even word boundaries, so the mem portion + (which is returned to the user) is also on an even word boundary, and + thus at least double-word aligned. + + The P (PINUSE_BIT) bit, stored in the unused low-order bit of the + chunk size (which is always a multiple of two words), is an in-use + bit for the *previous* chunk. If that bit is *clear*, then the + word before the current chunk size contains the previous chunk + size, and can be used to find the front of the previous chunk. + The very first chunk allocated always has this bit set, preventing + access to non-existent (or non-owned) memory. If pinuse is set for + any given chunk, then you CANNOT determine the size of the + previous chunk, and might even get a memory addressing fault when + trying to do so. + + The C (CINUSE_BIT) bit, stored in the unused second-lowest bit of + the chunk size redundantly records whether the current chunk is + inuse (unless the chunk is mmapped). This redundancy enables usage + checks within free and realloc, and reduces indirection when freeing + and consolidating chunks. + + Each freshly allocated chunk must have both cinuse and pinuse set. + That is, each allocated chunk borders either a previously allocated + and still in-use chunk, or the base of its memory arena. This is + ensured by making all allocations from the `lowest' part of any + found chunk. Further, no free chunk physically borders another one, + so each free chunk is known to be preceded and followed by either + inuse chunks or the ends of memory. + + Note that the `foot' of the current chunk is actually represented + as the prev_foot of the NEXT chunk. This makes it easier to + deal with alignments etc but can be very confusing when trying + to extend or adapt this code. + + The exceptions to all this are + + 1. The special chunk `top' is the top-most available chunk (i.e., + the one bordering the end of available memory). It is treated + specially. Top is never included in any bin, is used only if + no other chunk is available, and is released back to the + system if it is very large (see M_TRIM_THRESHOLD). In effect, + the top chunk is treated as larger (and thus less well + fitting) than any other available chunk. The top chunk + doesn't update its trailing size field since there is no next + contiguous chunk that would have to index off it. However, + space is still allocated for it (TOP_FOOT_SIZE) to enable + separation or merging when space is extended. + + 3. Chunks allocated via mmap, have both cinuse and pinuse bits + cleared in their head fields. Because they are allocated + one-by-one, each must carry its own prev_foot field, which is + also used to hold the offset this chunk has within its mmapped + region, which is needed to preserve alignment. Each mmapped + chunk is trailed by the first two fields of a fake next-chunk + for sake of usage checks. + + */ struct malloc_chunk { - size_t prev_foot; /* Size of previous chunk (if free). */ - size_t head; /* Size and inuse bits. */ - struct malloc_chunk* fd; /* double links -- used only if free. */ - struct malloc_chunk* bk; + size_t prev_foot; /* Size of previous chunk (if free). */ + size_t head; /* Size and inuse bits. */ + struct malloc_chunk* fd; /* double links -- used only if free. */ + struct malloc_chunk* bk; }; typedef struct malloc_chunk mchunk; @@ -2089,8 +2223,8 @@ typedef unsigned int flag_t; /* The type of various bit flag sets */ #define MMAP_FOOT_PAD (FOUR_SIZE_T_SIZES) /* The smallest size we can malloc is an aligned minimal chunk */ -#define MIN_CHUNK_SIZE\ - ((MCHUNK_SIZE + CHUNK_ALIGN_MASK) & ~CHUNK_ALIGN_MASK) +#define MIN_CHUNK_SIZE \ +((MCHUNK_SIZE + CHUNK_ALIGN_MASK) & ~CHUNK_ALIGN_MASK) /* conversion from malloc headers to user pointers, and back */ #define chunk2mem(p) ((void*)((char*)(p) + TWO_SIZE_T_SIZES)) @@ -2104,22 +2238,22 @@ typedef unsigned int flag_t; /* The type of various bit flag sets */ /* pad request bytes into a usable size */ #define pad_request(req) \ - (((req) + CHUNK_OVERHEAD + CHUNK_ALIGN_MASK) & ~CHUNK_ALIGN_MASK) +(((req) + CHUNK_OVERHEAD + CHUNK_ALIGN_MASK) & ~CHUNK_ALIGN_MASK) /* pad request, checking for minimum (but not maximum) */ #define request2size(req) \ - (((req) < MIN_REQUEST)? MIN_CHUNK_SIZE : pad_request(req)) +(((req) < MIN_REQUEST)? MIN_CHUNK_SIZE : pad_request(req)) /* ------------------ Operations on head and foot fields ----------------- */ /* - The head field of a chunk is or'ed with PINUSE_BIT when previous - adjacent chunk in use, and or'ed with CINUSE_BIT if this chunk is in - use, unless mmapped, in which case both bits are cleared. - - FLAG4_BIT is not used by this malloc, but might be useful in extensions. -*/ + The head field of a chunk is or'ed with PINUSE_BIT when previous + adjacent chunk in use, and or'ed with CINUSE_BIT if this chunk is in + use, unless mmapped, in which case both bits are cleared. + + FLAG4_BIT is not used by this malloc, but might be useful in extensions. + */ #define PINUSE_BIT (SIZE_T_ONE) #define CINUSE_BIT (SIZE_T_TWO) @@ -2133,12 +2267,15 @@ typedef unsigned int flag_t; /* The type of various bit flag sets */ /* extraction of fields from head words */ #define cinuse(p) ((p)->head & CINUSE_BIT) #define pinuse(p) ((p)->head & PINUSE_BIT) +#define flag4inuse(p) ((p)->head & FLAG4_BIT) #define is_inuse(p) (((p)->head & INUSE_BITS) != PINUSE_BIT) #define is_mmapped(p) (((p)->head & INUSE_BITS) == 0) #define chunksize(p) ((p)->head & ~(FLAG_BITS)) #define clear_pinuse(p) ((p)->head &= ~PINUSE_BIT) +#define set_flag4(p) ((p)->head |= FLAG4_BIT) +#define clear_flag4(p) ((p)->head &= ~FLAG4_BIT) /* Treat space at ptr +/- offset as a chunk */ #define chunk_plus_offset(p, s) ((mchunkptr)(((char*)(p)) + (s))) @@ -2157,15 +2294,15 @@ typedef unsigned int flag_t; /* The type of various bit flag sets */ /* Set size, pinuse bit, and foot */ #define set_size_and_pinuse_of_free_chunk(p, s)\ - ((p)->head = (s|PINUSE_BIT), set_foot(p, s)) +((p)->head = (s|PINUSE_BIT), set_foot(p, s)) /* Set size, pinuse bit, foot, and clear next pinuse */ #define set_free_with_pinuse(p, s, n)\ - (clear_pinuse(n), set_size_and_pinuse_of_free_chunk(p, s)) +(clear_pinuse(n), set_size_and_pinuse_of_free_chunk(p, s)) /* Get the internal overhead associated with chunk p */ #define overhead_for(p)\ - (is_mmapped(p)? MMAP_CHUNK_OVERHEAD : CHUNK_OVERHEAD) +(is_mmapped(p)? MMAP_CHUNK_OVERHEAD : CHUNK_OVERHEAD) /* Return true if malloced space is not necessarily cleared */ #if MMAP_CLEARS @@ -2177,104 +2314,104 @@ typedef unsigned int flag_t; /* The type of various bit flag sets */ /* ---------------------- Overlaid data structures ----------------------- */ /* - When chunks are not in use, they are treated as nodes of either - lists or trees. - - "Small" chunks are stored in circular doubly-linked lists, and look - like this: - - chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - | Size of previous chunk | - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - `head:' | Size of chunk, in bytes |P| - mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - | Forward pointer to next chunk in list | - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - | Back pointer to previous chunk in list | - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - | Unused space (may be 0 bytes long) . - . . - . | -nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - `foot:' | Size of chunk, in bytes | - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - - Larger chunks are kept in a form of bitwise digital trees (aka - tries) keyed on chunksizes. Because malloc_tree_chunks are only for - free chunks greater than 256 bytes, their size doesn't impose any - constraints on user chunk sizes. Each node looks like: - - chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - | Size of previous chunk | - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - `head:' | Size of chunk, in bytes |P| - mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - | Forward pointer to next chunk of same size | - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - | Back pointer to previous chunk of same size | - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - | Pointer to left child (child[0]) | - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - | Pointer to right child (child[1]) | - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - | Pointer to parent | - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - | bin index of this chunk | - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - | Unused space . - . | -nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - `foot:' | Size of chunk, in bytes | - +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ - - Each tree holding treenodes is a tree of unique chunk sizes. Chunks - of the same size are arranged in a circularly-linked list, with only - the oldest chunk (the next to be used, in our FIFO ordering) - actually in the tree. (Tree members are distinguished by a non-null - parent pointer.) If a chunk with the same size an an existing node - is inserted, it is linked off the existing node using pointers that - work in the same way as fd/bk pointers of small chunks. - - Each tree contains a power of 2 sized range of chunk sizes (the - smallest is 0x100 <= x < 0x180), which is is divided in half at each - tree level, with the chunks in the smaller half of the range (0x100 - <= x < 0x140 for the top nose) in the left subtree and the larger - half (0x140 <= x < 0x180) in the right subtree. This is, of course, - done by inspecting individual bits. - - Using these rules, each node's left subtree contains all smaller - sizes than its right subtree. However, the node at the root of each - subtree has no particular ordering relationship to either. (The - dividing line between the subtree sizes is based on trie relation.) - If we remove the last chunk of a given size from the interior of the - tree, we need to replace it with a leaf node. The tree ordering - rules permit a node to be replaced by any leaf below it. - - The smallest chunk in a tree (a common operation in a best-fit - allocator) can be found by walking a path to the leftmost leaf in - the tree. Unlike a usual binary tree, where we follow left child - pointers until we reach a null, here we follow the right child - pointer any time the left one is null, until we reach a leaf with - both child pointers null. The smallest chunk in the tree will be - somewhere along that path. - - The worst case number of steps to add, find, or remove a node is - bounded by the number of bits differentiating chunks within - bins. Under current bin calculations, this ranges from 6 up to 21 - (for 32 bit sizes) or up to 53 (for 64 bit sizes). The typical case - is of course much better. -*/ + When chunks are not in use, they are treated as nodes of either + lists or trees. + + "Small" chunks are stored in circular doubly-linked lists, and look + like this: + + chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Size of previous chunk | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + `head:' | Size of chunk, in bytes |P| + mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Forward pointer to next chunk in list | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Back pointer to previous chunk in list | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Unused space (may be 0 bytes long) . + . . + . | + nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + `foot:' | Size of chunk, in bytes | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + + Larger chunks are kept in a form of bitwise digital trees (aka + tries) keyed on chunksizes. Because malloc_tree_chunks are only for + free chunks greater than 256 bytes, their size doesn't impose any + constraints on user chunk sizes. Each node looks like: + + chunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Size of previous chunk | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + `head:' | Size of chunk, in bytes |P| + mem-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Forward pointer to next chunk of same size | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Back pointer to previous chunk of same size | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Pointer to left child (child[0]) | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Pointer to right child (child[1]) | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Pointer to parent | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | bin index of this chunk | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + | Unused space . + . | + nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + `foot:' | Size of chunk, in bytes | + +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + + Each tree holding treenodes is a tree of unique chunk sizes. Chunks + of the same size are arranged in a circularly-linked list, with only + the oldest chunk (the next to be used, in our FIFO ordering) + actually in the tree. (Tree members are distinguished by a non-null + parent pointer.) If a chunk with the same size an an existing node + is inserted, it is linked off the existing node using pointers that + work in the same way as fd/bk pointers of small chunks. + + Each tree contains a power of 2 sized range of chunk sizes (the + smallest is 0x100 <= x < 0x180), which is is divided in half at each + tree level, with the chunks in the smaller half of the range (0x100 + <= x < 0x140 for the top nose) in the left subtree and the larger + half (0x140 <= x < 0x180) in the right subtree. This is, of course, + done by inspecting individual bits. + + Using these rules, each node's left subtree contains all smaller + sizes than its right subtree. However, the node at the root of each + subtree has no particular ordering relationship to either. (The + dividing line between the subtree sizes is based on trie relation.) + If we remove the last chunk of a given size from the interior of the + tree, we need to replace it with a leaf node. The tree ordering + rules permit a node to be replaced by any leaf below it. + + The smallest chunk in a tree (a common operation in a best-fit + allocator) can be found by walking a path to the leftmost leaf in + the tree. Unlike a usual binary tree, where we follow left child + pointers until we reach a null, here we follow the right child + pointer any time the left one is null, until we reach a leaf with + both child pointers null. The smallest chunk in the tree will be + somewhere along that path. + + The worst case number of steps to add, find, or remove a node is + bounded by the number of bits differentiating chunks within + bins. Under current bin calculations, this ranges from 6 up to 21 + (for 32 bit sizes) or up to 53 (for 64 bit sizes). The typical case + is of course much better. + */ struct malloc_tree_chunk { - /* The first four fields must be compatible with malloc_chunk */ - size_t prev_foot; - size_t head; - struct malloc_tree_chunk* fd; - struct malloc_tree_chunk* bk; - - struct malloc_tree_chunk* child[2]; - struct malloc_tree_chunk* parent; - bindex_t index; + /* The first four fields must be compatible with malloc_chunk */ + size_t prev_foot; + size_t head; + struct malloc_tree_chunk* fd; + struct malloc_tree_chunk* bk; + + struct malloc_tree_chunk* child[2]; + struct malloc_tree_chunk* parent; + bindex_t index; }; typedef struct malloc_tree_chunk tchunk; @@ -2287,65 +2424,65 @@ typedef struct malloc_tree_chunk* tbinptr; /* The type of bins of trees */ /* ----------------------------- Segments -------------------------------- */ /* - Each malloc space may include non-contiguous segments, held in a - list headed by an embedded malloc_segment record representing the - top-most space. Segments also include flags holding properties of - the space. Large chunks that are directly allocated by mmap are not - included in this list. They are instead independently created and - destroyed without otherwise keeping track of them. - - Segment management mainly comes into play for spaces allocated by - MMAP. Any call to MMAP might or might not return memory that is - adjacent to an existing segment. MORECORE normally contiguously - extends the current space, so this space is almost always adjacent, - which is simpler and faster to deal with. (This is why MORECORE is - used preferentially to MMAP when both are available -- see - sys_alloc.) When allocating using MMAP, we don't use any of the - hinting mechanisms (inconsistently) supported in various - implementations of unix mmap, or distinguish reserving from - committing memory. Instead, we just ask for space, and exploit - contiguity when we get it. It is probably possible to do - better than this on some systems, but no general scheme seems - to be significantly better. - - Management entails a simpler variant of the consolidation scheme - used for chunks to reduce fragmentation -- new adjacent memory is - normally prepended or appended to an existing segment. However, - there are limitations compared to chunk consolidation that mostly - reflect the fact that segment processing is relatively infrequent - (occurring only when getting memory from system) and that we - don't expect to have huge numbers of segments: - - * Segments are not indexed, so traversal requires linear scans. (It - would be possible to index these, but is not worth the extra - overhead and complexity for most programs on most platforms.) - * New segments are only appended to old ones when holding top-most - memory; if they cannot be prepended to others, they are held in - different segments. - - Except for the top-most segment of an mstate, each segment record - is kept at the tail of its segment. Segments are added by pushing - segment records onto the list headed by &mstate.seg for the - containing mstate. - - Segment flags control allocation/merge/deallocation policies: - * If EXTERN_BIT set, then we did not allocate this segment, - and so should not try to deallocate or merge with others. - (This currently holds only for the initial segment passed - into create_mspace_with_base.) - * If USE_MMAP_BIT set, the segment may be merged with - other surrounding mmapped segments and trimmed/de-allocated - using munmap. - * If neither bit is set, then the segment was obtained using - MORECORE so can be merged with surrounding MORECORE'd segments - and deallocated/trimmed using MORECORE with negative arguments. -*/ + Each malloc space may include non-contiguous segments, held in a + list headed by an embedded malloc_segment record representing the + top-most space. Segments also include flags holding properties of + the space. Large chunks that are directly allocated by mmap are not + included in this list. They are instead independently created and + destroyed without otherwise keeping track of them. + + Segment management mainly comes into play for spaces allocated by + MMAP. Any call to MMAP might or might not return memory that is + adjacent to an existing segment. MORECORE normally contiguously + extends the current space, so this space is almost always adjacent, + which is simpler and faster to deal with. (This is why MORECORE is + used preferentially to MMAP when both are available -- see + sys_alloc.) When allocating using MMAP, we don't use any of the + hinting mechanisms (inconsistently) supported in various + implementations of unix mmap, or distinguish reserving from + committing memory. Instead, we just ask for space, and exploit + contiguity when we get it. It is probably possible to do + better than this on some systems, but no general scheme seems + to be significantly better. + + Management entails a simpler variant of the consolidation scheme + used for chunks to reduce fragmentation -- new adjacent memory is + normally prepended or appended to an existing segment. However, + there are limitations compared to chunk consolidation that mostly + reflect the fact that segment processing is relatively infrequent + (occurring only when getting memory from system) and that we + don't expect to have huge numbers of segments: + + * Segments are not indexed, so traversal requires linear scans. (It + would be possible to index these, but is not worth the extra + overhead and complexity for most programs on most platforms.) + * New segments are only appended to old ones when holding top-most + memory; if they cannot be prepended to others, they are held in + different segments. + + Except for the top-most segment of an mstate, each segment record + is kept at the tail of its segment. Segments are added by pushing + segment records onto the list headed by &mstate.seg for the + containing mstate. + + Segment flags control allocation/merge/deallocation policies: + * If EXTERN_BIT set, then we did not allocate this segment, + and so should not try to deallocate or merge with others. + (This currently holds only for the initial segment passed + into create_mspace_with_base.) + * If USE_MMAP_BIT set, the segment may be merged with + other surrounding mmapped segments and trimmed/de-allocated + using munmap. + * If neither bit is set, then the segment was obtained using + MORECORE so can be merged with surrounding MORECORE'd segments + and deallocated/trimmed using MORECORE with negative arguments. + */ struct malloc_segment { - char* base; /* base address */ - size_t size; /* allocated size */ - struct malloc_segment* next; /* ptr to next segment */ - flag_t sflags; /* mmap and extern flag */ + char* base; /* base address */ + size_t size; /* allocated size */ + struct malloc_segment* next; /* ptr to next segment */ + flag_t sflags; /* mmap and extern flag */ }; #define is_mmapped_segment(S) ((S)->sflags & USE_MMAP_BIT) @@ -2357,86 +2494,89 @@ typedef struct malloc_segment* msegmentptr; /* ---------------------------- malloc_state ----------------------------- */ /* - A malloc_state holds all of the bookkeeping for a space. - The main fields are: - - Top - The topmost chunk of the currently active segment. Its size is - cached in topsize. The actual size of topmost space is - topsize+TOP_FOOT_SIZE, which includes space reserved for adding - fenceposts and segment records if necessary when getting more - space from the system. The size at which to autotrim top is - cached from mparams in trim_check, except that it is disabled if - an autotrim fails. - - Designated victim (dv) - This is the preferred chunk for servicing small requests that - don't have exact fits. It is normally the chunk split off most - recently to service another small request. Its size is cached in - dvsize. The link fields of this chunk are not maintained since it - is not kept in a bin. - - SmallBins - An array of bin headers for free chunks. These bins hold chunks - with sizes less than MIN_LARGE_SIZE bytes. Each bin contains - chunks of all the same size, spaced 8 bytes apart. To simplify - use in double-linked lists, each bin header acts as a malloc_chunk - pointing to the real first node, if it exists (else pointing to - itself). This avoids special-casing for headers. But to avoid - waste, we allocate only the fd/bk pointers of bins, and then use - repositioning tricks to treat these as the fields of a chunk. - - TreeBins - Treebins are pointers to the roots of trees holding a range of - sizes. There are 2 equally spaced treebins for each power of two - from TREE_SHIFT to TREE_SHIFT+16. The last bin holds anything - larger. - - Bin maps - There is one bit map for small bins ("smallmap") and one for - treebins ("treemap). Each bin sets its bit when non-empty, and - clears the bit when empty. Bit operations are then used to avoid - bin-by-bin searching -- nearly all "search" is done without ever - looking at bins that won't be selected. The bit maps - conservatively use 32 bits per map word, even if on 64bit system. - For a good description of some of the bit-based techniques used - here, see Henry S. Warren Jr's book "Hacker's Delight" (and - supplement at http://hackersdelight.org/). Many of these are - intended to reduce the branchiness of paths through malloc etc, as - well as to reduce the number of memory locations read or written. - - Segments - A list of segments headed by an embedded malloc_segment record - representing the initial space. - - Address check support - The least_addr field is the least address ever obtained from - MORECORE or MMAP. Attempted frees and reallocs of any address less - than this are trapped (unless INSECURE is defined). - - Magic tag - A cross-check field that should always hold same value as mparams.magic. - - Flags - Bits recording whether to use MMAP, locks, or contiguous MORECORE - - Statistics - Each space keeps track of current and maximum system memory - obtained via MORECORE or MMAP. - - Trim support - Fields holding the amount of unused topmost memory that should trigger - timming, and a counter to force periodic scanning to release unused - non-topmost segments. - - Locking - If USE_LOCKS is defined, the "mutex" lock is acquired and released - around every public call using this mspace. - - Extension support - A void* pointer and a size_t field that can be used to help implement - extensions to this malloc. -*/ + A malloc_state holds all of the bookkeeping for a space. + The main fields are: + + Top + The topmost chunk of the currently active segment. Its size is + cached in topsize. The actual size of topmost space is + topsize+TOP_FOOT_SIZE, which includes space reserved for adding + fenceposts and segment records if necessary when getting more + space from the system. The size at which to autotrim top is + cached from mparams in trim_check, except that it is disabled if + an autotrim fails. + + Designated victim (dv) + This is the preferred chunk for servicing small requests that + don't have exact fits. It is normally the chunk split off most + recently to service another small request. Its size is cached in + dvsize. The link fields of this chunk are not maintained since it + is not kept in a bin. + + SmallBins + An array of bin headers for free chunks. These bins hold chunks + with sizes less than MIN_LARGE_SIZE bytes. Each bin contains + chunks of all the same size, spaced 8 bytes apart. To simplify + use in double-linked lists, each bin header acts as a malloc_chunk + pointing to the real first node, if it exists (else pointing to + itself). This avoids special-casing for headers. But to avoid + waste, we allocate only the fd/bk pointers of bins, and then use + repositioning tricks to treat these as the fields of a chunk. + + TreeBins + Treebins are pointers to the roots of trees holding a range of + sizes. There are 2 equally spaced treebins for each power of two + from TREE_SHIFT to TREE_SHIFT+16. The last bin holds anything + larger. + + Bin maps + There is one bit map for small bins ("smallmap") and one for + treebins ("treemap). Each bin sets its bit when non-empty, and + clears the bit when empty. Bit operations are then used to avoid + bin-by-bin searching -- nearly all "search" is done without ever + looking at bins that won't be selected. The bit maps + conservatively use 32 bits per map word, even if on 64bit system. + For a good description of some of the bit-based techniques used + here, see Henry S. Warren Jr's book "Hacker's Delight" (and + supplement at http://hackersdelight.org/). Many of these are + intended to reduce the branchiness of paths through malloc etc, as + well as to reduce the number of memory locations read or written. + + Segments + A list of segments headed by an embedded malloc_segment record + representing the initial space. + + Address check support + The least_addr field is the least address ever obtained from + MORECORE or MMAP. Attempted frees and reallocs of any address less + than this are trapped (unless INSECURE is defined). + + Magic tag + A cross-check field that should always hold same value as mparams.magic. + + Max allowed footprint + The maximum allowed bytes to allocate from system (zero means no limit) + + Flags + Bits recording whether to use MMAP, locks, or contiguous MORECORE + + Statistics + Each space keeps track of current and maximum system memory + obtained via MORECORE or MMAP. + + Trim support + Fields holding the amount of unused topmost memory that should trigger + trimming, and a counter to force periodic scanning to release unused + non-topmost segments. + + Locking + If USE_LOCKS is defined, the "mutex" lock is acquired and released + around every public call using this mspace. + + Extension support + A void* pointer and a size_t field that can be used to help implement + extensions to this malloc. + */ /* Bin types, widths and sizes */ #define NSMALLBINS (32U) @@ -2449,27 +2589,28 @@ typedef struct malloc_segment* msegmentptr; #define MAX_SMALL_REQUEST (MAX_SMALL_SIZE - CHUNK_ALIGN_MASK - CHUNK_OVERHEAD) struct malloc_state { - binmap_t smallmap; - binmap_t treemap; - size_t dvsize; - size_t topsize; - char* least_addr; - mchunkptr dv; - mchunkptr top; - size_t trim_check; - size_t release_checks; - size_t magic; - mchunkptr smallbins[(NSMALLBINS+1)*2]; - tbinptr treebins[NTREEBINS]; - size_t footprint; - size_t max_footprint; - flag_t mflags; + binmap_t smallmap; + binmap_t treemap; + size_t dvsize; + size_t topsize; + char* least_addr; + mchunkptr dv; + mchunkptr top; + size_t trim_check; + size_t release_checks; + size_t magic; + mchunkptr smallbins[(NSMALLBINS+1)*2]; + tbinptr treebins[NTREEBINS]; + size_t footprint; + size_t max_footprint; + size_t footprint_limit; /* zero means no limit */ + flag_t mflags; #if USE_LOCKS - MLOCK_T mutex; /* locate lock among fields that rarely change */ + MLOCK_T mutex; /* locate lock among fields that rarely change */ #endif /* USE_LOCKS */ - msegment seg; - void* extp; /* Unused but available for extensions */ - size_t exts; + msegment seg; + void* extp; /* Unused but available for extensions */ + size_t exts; }; typedef struct malloc_state* mstate; @@ -2477,19 +2618,19 @@ typedef struct malloc_state* mstate; /* ------------- Global malloc_state and malloc_params ------------------- */ /* - malloc_params holds global properties, including those that can be - dynamically set using mallopt. There is a single instance, mparams, - initialized in init_mparams. Note that the non-zeroness of "magic" - also serves as an initialization flag. -*/ + malloc_params holds global properties, including those that can be + dynamically set using mallopt. There is a single instance, mparams, + initialized in init_mparams. Note that the non-zeroness of "magic" + also serves as an initialization flag. + */ struct malloc_params { - volatile size_t magic; - size_t page_size; - size_t granularity; - size_t mmap_threshold; - size_t trim_threshold; - flag_t default_mflags; + size_t magic; + size_t page_size; + size_t granularity; + size_t mmap_threshold; + size_t trim_threshold; + flag_t default_mflags; }; static struct malloc_params mparams; @@ -2514,28 +2655,36 @@ static struct malloc_state _gm_; #define use_lock(M) ((M)->mflags & USE_LOCK_BIT) #define enable_lock(M) ((M)->mflags |= USE_LOCK_BIT) +#if USE_LOCKS #define disable_lock(M) ((M)->mflags &= ~USE_LOCK_BIT) +#else +#define disable_lock(M) +#endif #define use_mmap(M) ((M)->mflags & USE_MMAP_BIT) #define enable_mmap(M) ((M)->mflags |= USE_MMAP_BIT) +#if HAVE_MMAP #define disable_mmap(M) ((M)->mflags &= ~USE_MMAP_BIT) +#else +#define disable_mmap(M) +#endif #define use_noncontiguous(M) ((M)->mflags & USE_NONCONTIGUOUS_BIT) #define disable_contiguous(M) ((M)->mflags |= USE_NONCONTIGUOUS_BIT) #define set_lock(M,L)\ - ((M)->mflags = (L)?\ - ((M)->mflags | USE_LOCK_BIT) :\ - ((M)->mflags & ~USE_LOCK_BIT)) +((M)->mflags = (L)?\ +((M)->mflags | USE_LOCK_BIT) :\ +((M)->mflags & ~USE_LOCK_BIT)) /* page-align a size */ #define page_align(S)\ - (((S) + (mparams.page_size - SIZE_T_ONE)) & ~(mparams.page_size - SIZE_T_ONE)) +(((S) + (mparams.page_size - SIZE_T_ONE)) & ~(mparams.page_size - SIZE_T_ONE)) /* granularity-align a size */ #define granularity_align(S)\ - (((S) + (mparams.granularity - SIZE_T_ONE))\ - & ~(mparams.granularity - SIZE_T_ONE)) +(((S) + (mparams.granularity - SIZE_T_ONE))\ +& ~(mparams.granularity - SIZE_T_ONE)) /* For mmap, use granularity alignment on windows, else page-align */ @@ -2549,34 +2698,34 @@ static struct malloc_state _gm_; #define SYS_ALLOC_PADDING (TOP_FOOT_SIZE + MALLOC_ALIGNMENT) #define is_page_aligned(S)\ - (((size_t)(S) & (mparams.page_size - SIZE_T_ONE)) == 0) +(((size_t)(S) & (mparams.page_size - SIZE_T_ONE)) == 0) #define is_granularity_aligned(S)\ - (((size_t)(S) & (mparams.granularity - SIZE_T_ONE)) == 0) +(((size_t)(S) & (mparams.granularity - SIZE_T_ONE)) == 0) /* True if segment S holds address A */ #define segment_holds(S, A)\ - ((char*)(A) >= S->base && (char*)(A) < S->base + S->size) +((char*)(A) >= S->base && (char*)(A) < S->base + S->size) /* Return segment holding given address */ static msegmentptr segment_holding(mstate m, char* addr) { - msegmentptr sp = &m->seg; - for (;;) { - if (addr >= sp->base && addr < sp->base + sp->size) - return sp; - if ((sp = sp->next) == 0) - return 0; - } + msegmentptr sp = &m->seg; + for (;;) { + if (addr >= sp->base && addr < sp->base + sp->size) + return sp; + if ((sp = sp->next) == 0) + return 0; + } } /* Return true if segment contains a segment link */ static int has_segment_link(mstate m, msegmentptr ss) { - msegmentptr sp = &m->seg; - for (;;) { - if ((char*)sp >= ss->base && (char*)sp < ss->base + ss->size) - return 1; - if ((sp = sp->next) == 0) - return 0; - } + msegmentptr sp = &m->seg; + for (;;) { + if ((char*)sp >= ss->base && (char*)sp < ss->base + ss->size) + return 1; + if ((sp = sp->next) == 0) + return 0; + } } #ifndef MORECORE_CANNOT_TRIM @@ -2586,24 +2735,23 @@ static int has_segment_link(mstate m, msegmentptr ss) { #endif /* MORECORE_CANNOT_TRIM */ /* - TOP_FOOT_SIZE is padding at the end of a segment, including space - that may be needed to place segment records and fenceposts when new - noncontiguous segments are added. -*/ -#define TOP_FOOT_SIZE\ - (align_offset(chunk2mem(0))+pad_request(sizeof(struct malloc_segment))+MIN_CHUNK_SIZE) + TOP_FOOT_SIZE is padding at the end of a segment, including space + that may be needed to place segment records and fenceposts when new + noncontiguous segments are added. + */ +#define TOP_FOOT_SIZE \ +(align_offset(chunk2mem(0))+pad_request(sizeof(struct malloc_segment))+MIN_CHUNK_SIZE) /* ------------------------------- Hooks -------------------------------- */ /* - PREACTION should be defined to return 0 on success, and nonzero on - failure. If you are not using locking, you can redefine these to do - anything you like. -*/ + PREACTION should be defined to return 0 on success, and nonzero on + failure. If you are not using locking, you can redefine these to do + anything you like. + */ #if USE_LOCKS - #define PREACTION(M) ((use_lock(M))? ACQUIRE_LOCK(&(M)->mutex) : 0) #define POSTACTION(M) { if (use_lock(M)) RELEASE_LOCK(&(M)->mutex); } #else /* USE_LOCKS */ @@ -2619,12 +2767,12 @@ static int has_segment_link(mstate m, msegmentptr ss) { #endif /* USE_LOCKS */ /* - CORRUPTION_ERROR_ACTION is triggered upon detected bad addresses. - USAGE_ERROR_ACTION is triggered on detected bad frees and - reallocs. The argument p is an address that might have triggered the - fault. It is ignored by the two predefined actions, but might be - useful in custom actions that try to help diagnose errors. -*/ + CORRUPTION_ERROR_ACTION is triggered upon detected bad addresses. + USAGE_ERROR_ACTION is triggered on detected bad frees and + reallocs. The argument p is an address that might have triggered the + fault. It is ignored by the two predefined actions, but might be + useful in custom actions that try to help diagnose errors. + */ #if PROCEED_ON_ERROR @@ -2649,6 +2797,7 @@ static void reset_on_error(mstate m); #endif /* PROCEED_ON_ERROR */ + /* -------------------------- Debugging setup ---------------------------- */ #if ! DEBUG @@ -2685,7 +2834,7 @@ static size_t traverse_and_check(mstate m); /* ---------------------------- Indexing Bins ---------------------------- */ #define is_small(s) (((s) >> SMALLBIN_SHIFT) < NSMALLBINS) -#define small_index(s) ((s) >> SMALLBIN_SHIFT) +#define small_index(s) (bindex_t)((s) >> SMALLBIN_SHIFT) #define small_index2size(i) ((i) << SMALLBIN_SHIFT) #define MIN_SMALL_INDEX (small_index(MIN_CHUNK_SIZE)) @@ -2697,80 +2846,79 @@ static size_t traverse_and_check(mstate m); #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) #define compute_tree_index(S, I)\ {\ - unsigned int X = S >> TREEBIN_SHIFT;\ - if (X == 0)\ - I = 0;\ - else if (X > 0xFFFF)\ - I = NTREEBINS-1;\ - else {\ - unsigned int K;\ - __asm__("bsrl\t%1, %0\n\t" : "=r" (K) : "g" (X));\ - I = (bindex_t)((K << 1) + ((S >> (K + (TREEBIN_SHIFT-1)) & 1)));\ - }\ +unsigned int X = S >> TREEBIN_SHIFT;\ +if (X == 0)\ +I = 0;\ +else if (X > 0xFFFF)\ +I = NTREEBINS-1;\ +else {\ +unsigned int K = (unsigned) sizeof(X)*__CHAR_BIT__ - 1 - (unsigned) __builtin_clz(X); \ +I = (bindex_t)((K << 1) + ((S >> (K + (TREEBIN_SHIFT-1)) & 1)));\ +}\ } #elif defined (__INTEL_COMPILER) #define compute_tree_index(S, I)\ {\ - size_t X = S >> TREEBIN_SHIFT;\ - if (X == 0)\ - I = 0;\ - else if (X > 0xFFFF)\ - I = NTREEBINS-1;\ - else {\ - unsigned int K = _bit_scan_reverse (X); \ - I = (bindex_t)((K << 1) + ((S >> (K + (TREEBIN_SHIFT-1)) & 1)));\ - }\ +size_t X = S >> TREEBIN_SHIFT;\ +if (X == 0)\ +I = 0;\ +else if (X > 0xFFFF)\ +I = NTREEBINS-1;\ +else {\ +unsigned int K = _bit_scan_reverse (X); \ +I = (bindex_t)((K << 1) + ((S >> (K + (TREEBIN_SHIFT-1)) & 1)));\ +}\ } #elif defined(_MSC_VER) && _MSC_VER>=1300 #define compute_tree_index(S, I)\ {\ - size_t X = S >> TREEBIN_SHIFT;\ - if (X == 0)\ - I = 0;\ - else if (X > 0xFFFF)\ - I = NTREEBINS-1;\ - else {\ - unsigned int K;\ - _BitScanReverse((DWORD *) &K, X);\ - I = (bindex_t)((K << 1) + ((S >> (K + (TREEBIN_SHIFT-1)) & 1)));\ - }\ +size_t X = S >> TREEBIN_SHIFT;\ +if (X == 0)\ +I = 0;\ +else if (X > 0xFFFF)\ +I = NTREEBINS-1;\ +else {\ +unsigned int K;\ +_BitScanReverse((DWORD *) &K, (DWORD) X);\ +I = (bindex_t)((K << 1) + ((S >> (K + (TREEBIN_SHIFT-1)) & 1)));\ +}\ } #else /* GNUC */ #define compute_tree_index(S, I)\ {\ - size_t X = S >> TREEBIN_SHIFT;\ - if (X == 0)\ - I = 0;\ - else if (X > 0xFFFF)\ - I = NTREEBINS-1;\ - else {\ - unsigned int Y = (unsigned int)X;\ - unsigned int N = ((Y - 0x100) >> 16) & 8;\ - unsigned int K = (((Y <<= N) - 0x1000) >> 16) & 4;\ - N += K;\ - N += K = (((Y <<= K) - 0x4000) >> 16) & 2;\ - K = 14 - N + ((Y <<= K) >> 15);\ - I = (K << 1) + ((S >> (K + (TREEBIN_SHIFT-1)) & 1));\ - }\ +size_t X = S >> TREEBIN_SHIFT;\ +if (X == 0)\ +I = 0;\ +else if (X > 0xFFFF)\ +I = NTREEBINS-1;\ +else {\ +unsigned int Y = (unsigned int)X;\ +unsigned int N = ((Y - 0x100) >> 16) & 8;\ +unsigned int K = (((Y <<= N) - 0x1000) >> 16) & 4;\ +N += K;\ +N += K = (((Y <<= K) - 0x4000) >> 16) & 2;\ +K = 14 - N + ((Y <<= K) >> 15);\ +I = (K << 1) + ((S >> (K + (TREEBIN_SHIFT-1)) & 1));\ +}\ } #endif /* GNUC */ /* Bit representing maximum resolved size in a treebin at i */ #define bit_for_tree_index(i) \ - (i == NTREEBINS-1)? (SIZE_T_BITSIZE-1) : (((i) >> 1) + TREEBIN_SHIFT - 2) +(i == NTREEBINS-1)? (SIZE_T_BITSIZE-1) : (((i) >> 1) + TREEBIN_SHIFT - 2) /* Shift placing maximum resolved bit in a treebin at i as sign bit */ #define leftshift_for_tree_index(i) \ - ((i == NTREEBINS-1)? 0 : \ - ((SIZE_T_BITSIZE-SIZE_T_ONE) - (((i) >> 1) + TREEBIN_SHIFT - 2))) +((i == NTREEBINS-1)? 0 : \ +((SIZE_T_BITSIZE-SIZE_T_ONE) - (((i) >> 1) + TREEBIN_SHIFT - 2))) /* The size of the smallest chunk held in bin with index i */ #define minsize_for_tree_index(i) \ - ((SIZE_T_ONE << (((i) >> 1) + TREEBIN_SHIFT)) | \ - (((size_t)((i) & SIZE_T_ONE)) << (((i) >> 1) + TREEBIN_SHIFT - 1))) +((SIZE_T_ONE << (((i) >> 1) + TREEBIN_SHIFT)) | \ +(((size_t)((i) & SIZE_T_ONE)) << (((i) >> 1) + TREEBIN_SHIFT - 1))) /* ------------------------ Operations on bin maps ----------------------- */ @@ -2801,25 +2949,25 @@ static size_t traverse_and_check(mstate m); #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) #define compute_bit2idx(X, I)\ {\ - unsigned int J;\ - __asm__("bsfl\t%1, %0\n\t" : "=r" (J) : "g" (X));\ - I = (bindex_t)J;\ +unsigned int J;\ +J = __builtin_ctz(X); \ +I = (bindex_t)J;\ } #elif defined (__INTEL_COMPILER) #define compute_bit2idx(X, I)\ {\ - unsigned int J;\ - J = _bit_scan_forward (X); \ - I = (bindex_t)J;\ +unsigned int J;\ +J = _bit_scan_forward (X); \ +I = (bindex_t)J;\ } #elif defined(_MSC_VER) && _MSC_VER>=1300 #define compute_bit2idx(X, I)\ {\ - unsigned int J;\ - _BitScanForward((DWORD *) &J, X);\ - I = (bindex_t)J;\ +unsigned int J;\ +_BitScanForward((DWORD *) &J, X);\ +I = (bindex_t)J;\ } #elif USE_BUILTIN_FFS @@ -2828,14 +2976,14 @@ static size_t traverse_and_check(mstate m); #else #define compute_bit2idx(X, I)\ {\ - unsigned int Y = X - 1;\ - unsigned int K = Y >> (16-4) & 16;\ - unsigned int N = K; Y >>= K;\ - N += K = Y >> (8-3) & 8; Y >>= K;\ - N += K = Y >> (4-2) & 4; Y >>= K;\ - N += K = Y >> (2-1) & 2; Y >>= K;\ - N += K = Y >> (1-0) & 1; Y >>= K;\ - I = (bindex_t)(N + Y);\ +unsigned int Y = X - 1;\ +unsigned int K = Y >> (16-4) & 16;\ +unsigned int N = K; Y >>= K;\ +N += K = Y >> (8-3) & 8; Y >>= K;\ +N += K = Y >> (4-2) & 4; Y >>= K;\ +N += K = Y >> (2-1) & 2; Y >>= K;\ +N += K = Y >> (1-0) & 1; Y >>= K;\ +I = (bindex_t)(N + Y);\ } #endif /* GNUC */ @@ -2843,30 +2991,30 @@ static size_t traverse_and_check(mstate m); /* ----------------------- Runtime Check Support ------------------------- */ /* - For security, the main invariant is that malloc/free/etc never - writes to a static address other than malloc_state, unless static - malloc_state itself has been corrupted, which cannot occur via - malloc (because of these checks). In essence this means that we - believe all pointers, sizes, maps etc held in malloc_state, but - check all of those linked or offsetted from other embedded data - structures. These checks are interspersed with main code in a way - that tends to minimize their run-time cost. - - When FOOTERS is defined, in addition to range checking, we also - verify footer fields of inuse chunks, which can be used guarantee - that the mstate controlling malloc/free is intact. This is a - streamlined version of the approach described by William Robertson - et al in "Run-time Detection of Heap-based Overflows" LISA'03 - http://www.usenix.org/events/lisa03/tech/robertson.html The footer - of an inuse chunk holds the xor of its mstate and a random seed, - that is checked upon calls to free() and realloc(). This is - (probablistically) unguessable from outside the program, but can be - computed by any code successfully malloc'ing any chunk, so does not - itself provide protection against code that has already broken - security through some other means. Unlike Robertson et al, we - always dynamically check addresses of all offset chunks (previous, - next, etc). This turns out to be cheaper than relying on hashes. -*/ + For security, the main invariant is that malloc/free/etc never + writes to a static address other than malloc_state, unless static + malloc_state itself has been corrupted, which cannot occur via + malloc (because of these checks). In essence this means that we + believe all pointers, sizes, maps etc held in malloc_state, but + check all of those linked or offsetted from other embedded data + structures. These checks are interspersed with main code in a way + that tends to minimize their run-time cost. + + When FOOTERS is defined, in addition to range checking, we also + verify footer fields of inuse chunks, which can be used guarantee + that the mstate controlling malloc/free is intact. This is a + streamlined version of the approach described by William Robertson + et al in "Run-time Detection of Heap-based Overflows" LISA'03 + http://www.usenix.org/events/lisa03/tech/robertson.html The footer + of an inuse chunk holds the xor of its mstate and a random seed, + that is checked upon calls to free() and realloc(). This is + (probabalistically) unguessable from outside the program, but can be + computed by any code successfully malloc'ing any chunk, so does not + itself provide protection against code that has already broken + security through some other means. Unlike Robertson et al, we + always dynamically check addresses of all offset chunks (previous, + next, etc). This turns out to be cheaper than relying on hashes. + */ #if !INSECURE /* Check if address a is at least as high as any from MORECORE or MMAP */ @@ -2892,7 +3040,6 @@ static size_t traverse_and_check(mstate m); #define ok_magic(M) (1) #endif /* (FOOTERS && !INSECURE) */ - /* In gcc, use __builtin_expect to minimize impact of checks */ #if !INSECURE #if defined(__GNUC__) && __GNUC__ >= 3 @@ -2914,153 +3061,164 @@ static size_t traverse_and_check(mstate m); /* Set cinuse bit and pinuse bit of next chunk */ #define set_inuse(M,p,s)\ - ((p)->head = (((p)->head & PINUSE_BIT)|s|CINUSE_BIT),\ - ((mchunkptr)(((char*)(p)) + (s)))->head |= PINUSE_BIT) +((p)->head = (((p)->head & PINUSE_BIT)|s|CINUSE_BIT),\ +((mchunkptr)(((char*)(p)) + (s)))->head |= PINUSE_BIT) /* Set cinuse and pinuse of this chunk and pinuse of next chunk */ #define set_inuse_and_pinuse(M,p,s)\ - ((p)->head = (s|PINUSE_BIT|CINUSE_BIT),\ - ((mchunkptr)(((char*)(p)) + (s)))->head |= PINUSE_BIT) +((p)->head = (s|PINUSE_BIT|CINUSE_BIT),\ +((mchunkptr)(((char*)(p)) + (s)))->head |= PINUSE_BIT) /* Set size, cinuse and pinuse bit of this chunk */ #define set_size_and_pinuse_of_inuse_chunk(M, p, s)\ - ((p)->head = (s|PINUSE_BIT|CINUSE_BIT)) +((p)->head = (s|PINUSE_BIT|CINUSE_BIT)) #else /* FOOTERS */ /* Set foot of inuse chunk to be xor of mstate and seed */ #define mark_inuse_foot(M,p,s)\ - (((mchunkptr)((char*)(p) + (s)))->prev_foot = ((size_t)(M) ^ mparams.magic)) +(((mchunkptr)((char*)(p) + (s)))->prev_foot = ((size_t)(M) ^ mparams.magic)) #define get_mstate_for(p)\ - ((mstate)(((mchunkptr)((char*)(p) +\ - (chunksize(p))))->prev_foot ^ mparams.magic)) +((mstate)(((mchunkptr)((char*)(p) +\ +(chunksize(p))))->prev_foot ^ mparams.magic)) #define set_inuse(M,p,s)\ - ((p)->head = (((p)->head & PINUSE_BIT)|s|CINUSE_BIT),\ - (((mchunkptr)(((char*)(p)) + (s)))->head |= PINUSE_BIT), \ - mark_inuse_foot(M,p,s)) +((p)->head = (((p)->head & PINUSE_BIT)|s|CINUSE_BIT),\ +(((mchunkptr)(((char*)(p)) + (s)))->head |= PINUSE_BIT), \ +mark_inuse_foot(M,p,s)) #define set_inuse_and_pinuse(M,p,s)\ - ((p)->head = (s|PINUSE_BIT|CINUSE_BIT),\ - (((mchunkptr)(((char*)(p)) + (s)))->head |= PINUSE_BIT),\ - mark_inuse_foot(M,p,s)) +((p)->head = (s|PINUSE_BIT|CINUSE_BIT),\ +(((mchunkptr)(((char*)(p)) + (s)))->head |= PINUSE_BIT),\ +mark_inuse_foot(M,p,s)) #define set_size_and_pinuse_of_inuse_chunk(M, p, s)\ - ((p)->head = (s|PINUSE_BIT|CINUSE_BIT),\ - mark_inuse_foot(M, p, s)) +((p)->head = (s|PINUSE_BIT|CINUSE_BIT),\ +mark_inuse_foot(M, p, s)) #endif /* !FOOTERS */ /* ---------------------------- setting mparams -------------------------- */ +#if LOCK_AT_FORK +static void pre_fork(void) { ACQUIRE_LOCK(&(gm)->mutex); } +static void post_fork_parent(void) { RELEASE_LOCK(&(gm)->mutex); } +static void post_fork_child(void) { INITIAL_LOCK(&(gm)->mutex); } +#endif /* LOCK_AT_FORK */ + /* Initialize mparams */ static int init_mparams(void) { #ifdef NEED_GLOBAL_LOCK_INIT - if (malloc_global_mutex_status <= 0) - init_malloc_global_mutex(); + if (malloc_global_mutex_status <= 0) + init_malloc_global_mutex(); #endif - - ACQUIRE_MALLOC_GLOBAL_LOCK(); - if (mparams.magic == 0) { - size_t magic; - size_t psize; - size_t gsize; - + + ACQUIRE_MALLOC_GLOBAL_LOCK(); + if (mparams.magic == 0) { + size_t magic; + size_t psize; + size_t gsize; + #ifndef WIN32 - psize = malloc_getpagesize; - gsize = ((DEFAULT_GRANULARITY != 0)? DEFAULT_GRANULARITY : psize); + psize = malloc_getpagesize; + gsize = ((DEFAULT_GRANULARITY != 0)? DEFAULT_GRANULARITY : psize); #else /* WIN32 */ - { - SYSTEM_INFO system_info; - GetSystemInfo(&system_info); - psize = system_info.dwPageSize; - gsize = ((DEFAULT_GRANULARITY != 0)? - DEFAULT_GRANULARITY : system_info.dwAllocationGranularity); - } + { + SYSTEM_INFO system_info; + GetSystemInfo(&system_info); + psize = system_info.dwPageSize; + gsize = ((DEFAULT_GRANULARITY != 0)? + DEFAULT_GRANULARITY : system_info.dwAllocationGranularity); + } #endif /* WIN32 */ - - /* Sanity-check configuration: - size_t must be unsigned and as wide as pointer type. - ints must be at least 4 bytes. - alignment must be at least 8. - Alignment, min chunk size, and page size must all be powers of 2. - */ - if ((sizeof(size_t) != sizeof(char*)) || - (MAX_SIZE_T < MIN_CHUNK_SIZE) || - (sizeof(int) < 4) || - (MALLOC_ALIGNMENT < (size_t)8U) || - ((MALLOC_ALIGNMENT & (MALLOC_ALIGNMENT-SIZE_T_ONE)) != 0) || - ((MCHUNK_SIZE & (MCHUNK_SIZE-SIZE_T_ONE)) != 0) || - ((gsize & (gsize-SIZE_T_ONE)) != 0) || - ((psize & (psize-SIZE_T_ONE)) != 0)) - ABORT; - - mparams.granularity = gsize; - mparams.page_size = psize; - mparams.mmap_threshold = DEFAULT_MMAP_THRESHOLD; - mparams.trim_threshold = DEFAULT_TRIM_THRESHOLD; + + /* Sanity-check configuration: + size_t must be unsigned and as wide as pointer type. + ints must be at least 4 bytes. + alignment must be at least 8. + Alignment, min chunk size, and page size must all be powers of 2. + */ + if ((sizeof(size_t) != sizeof(char*)) || + (MAX_SIZE_T < MIN_CHUNK_SIZE) || + (sizeof(int) < 4) || + (MALLOC_ALIGNMENT < (size_t)8U) || + ((MALLOC_ALIGNMENT & (MALLOC_ALIGNMENT-SIZE_T_ONE)) != 0) || + ((MCHUNK_SIZE & (MCHUNK_SIZE-SIZE_T_ONE)) != 0) || + ((gsize & (gsize-SIZE_T_ONE)) != 0) || + ((psize & (psize-SIZE_T_ONE)) != 0)) + ABORT; + mparams.granularity = gsize; + mparams.page_size = psize; + mparams.mmap_threshold = DEFAULT_MMAP_THRESHOLD; + mparams.trim_threshold = DEFAULT_TRIM_THRESHOLD; #if MORECORE_CONTIGUOUS - mparams.default_mflags = USE_LOCK_BIT|USE_MMAP_BIT; + mparams.default_mflags = USE_LOCK_BIT|USE_MMAP_BIT; #else /* MORECORE_CONTIGUOUS */ - mparams.default_mflags = USE_LOCK_BIT|USE_MMAP_BIT|USE_NONCONTIGUOUS_BIT; + mparams.default_mflags = USE_LOCK_BIT|USE_MMAP_BIT|USE_NONCONTIGUOUS_BIT; #endif /* MORECORE_CONTIGUOUS */ - + #if !ONLY_MSPACES - /* Set up lock for main malloc area */ - gm->mflags = mparams.default_mflags; - INITIAL_LOCK(&gm->mutex); + /* Set up lock for main malloc area */ + gm->mflags = mparams.default_mflags; + (void)INITIAL_LOCK(&gm->mutex); #endif - - { +#if LOCK_AT_FORK + pthread_atfork(&pre_fork, &post_fork_parent, &post_fork_child); +#endif + + { #if USE_DEV_RANDOM - int fd; - unsigned char buf[sizeof(size_t)]; - /* Try to use /dev/urandom, else fall back on using time */ - if ((fd = open("/dev/urandom", O_RDONLY)) >= 0 && - read(fd, buf, sizeof(buf)) == sizeof(buf)) { - magic = *((size_t *) buf); - close(fd); - } - else + int fd; + unsigned char buf[sizeof(size_t)]; + /* Try to use /dev/urandom, else fall back on using time */ + if ((fd = open("/dev/urandom", O_RDONLY)) >= 0 && + read(fd, buf, sizeof(buf)) == sizeof(buf)) { + magic = *((size_t *) buf); + close(fd); + } + else #endif /* USE_DEV_RANDOM */ #ifdef WIN32 - magic = (size_t)(GetTickCount() ^ (size_t)0x55555555U); + magic = (size_t)(GetTickCount() ^ (size_t)0x55555555U); +#elif defined(LACKS_TIME_H) + magic = (size_t)&magic ^ (size_t)0x55555555U; #else - magic = (size_t)(time(0) ^ (size_t)0x55555555U); + magic = (size_t)(time(0) ^ (size_t)0x55555555U); #endif - magic |= (size_t)8U; /* ensure nonzero */ - magic &= ~(size_t)7U; /* improve chances of fault for bad values */ - mparams.magic = magic; + magic |= (size_t)8U; /* ensure nonzero */ + magic &= ~(size_t)7U; /* improve chances of fault for bad values */ + /* Until memory modes commonly available, use volatile-write */ + (*(volatile size_t *)(&(mparams.magic))) = magic; + } } - } - - RELEASE_MALLOC_GLOBAL_LOCK(); - return 1; + + RELEASE_MALLOC_GLOBAL_LOCK(); + return 1; } /* support for mallopt */ static int change_mparam(int param_number, int value) { - size_t val; - ensure_initialization(); - val = (value == -1)? MAX_SIZE_T : (size_t)value; - switch(param_number) { - case M_TRIM_THRESHOLD: - mparams.trim_threshold = val; - return 1; - case M_GRANULARITY: - if (val >= mparams.page_size && ((val & (val-1)) == 0)) { - mparams.granularity = val; - return 1; + size_t val; + ensure_initialization(); + val = (value == -1)? MAX_SIZE_T : (size_t)value; + switch(param_number) { + case M_TRIM_THRESHOLD: + mparams.trim_threshold = val; + return 1; + case M_GRANULARITY: + if (val >= mparams.page_size && ((val & (val-1)) == 0)) { + mparams.granularity = val; + return 1; + } + else + return 0; + case M_MMAP_THRESHOLD: + mparams.mmap_threshold = val; + return 1; + default: + return 0; } - else - return 0; - case M_MMAP_THRESHOLD: - mparams.mmap_threshold = val; - return 1; - default: - return 0; - } } #if DEBUG @@ -3068,266 +3226,267 @@ static int change_mparam(int param_number, int value) { /* Check properties of any chunk, whether free, inuse, mmapped etc */ static void do_check_any_chunk(mstate m, mchunkptr p) { - assert((is_aligned(chunk2mem(p))) || (p->head == FENCEPOST_HEAD)); - assert(ok_address(m, p)); + assert((is_aligned(chunk2mem(p))) || (p->head == FENCEPOST_HEAD)); + assert(ok_address(m, p)); } /* Check properties of top chunk */ static void do_check_top_chunk(mstate m, mchunkptr p) { - msegmentptr sp = segment_holding(m, (char*)p); - size_t sz = p->head & ~INUSE_BITS; /* third-lowest bit can be set! */ - assert(sp != 0); - assert((is_aligned(chunk2mem(p))) || (p->head == FENCEPOST_HEAD)); - assert(ok_address(m, p)); - assert(sz == m->topsize); - assert(sz > 0); - assert(sz == ((sp->base + sp->size) - (char*)p) - TOP_FOOT_SIZE); - assert(pinuse(p)); - assert(!pinuse(chunk_plus_offset(p, sz))); + msegmentptr sp = segment_holding(m, (char*)p); + size_t sz = p->head & ~INUSE_BITS; /* third-lowest bit can be set! */ + assert(sp != 0); + assert((is_aligned(chunk2mem(p))) || (p->head == FENCEPOST_HEAD)); + assert(ok_address(m, p)); + assert(sz == m->topsize); + assert(sz > 0); + assert(sz == ((sp->base + sp->size) - (char*)p) - TOP_FOOT_SIZE); + assert(pinuse(p)); + assert(!pinuse(chunk_plus_offset(p, sz))); } /* Check properties of (inuse) mmapped chunks */ static void do_check_mmapped_chunk(mstate m, mchunkptr p) { - size_t sz = chunksize(p); - size_t len = (sz + (p->prev_foot) + MMAP_FOOT_PAD); - assert(is_mmapped(p)); - assert(use_mmap(m)); - assert((is_aligned(chunk2mem(p))) || (p->head == FENCEPOST_HEAD)); - assert(ok_address(m, p)); - assert(!is_small(sz)); - assert((len & (mparams.page_size-SIZE_T_ONE)) == 0); - assert(chunk_plus_offset(p, sz)->head == FENCEPOST_HEAD); - assert(chunk_plus_offset(p, sz+SIZE_T_SIZE)->head == 0); + size_t sz = chunksize(p); + size_t len = (sz + (p->prev_foot) + MMAP_FOOT_PAD); + assert(is_mmapped(p)); + assert(use_mmap(m)); + assert((is_aligned(chunk2mem(p))) || (p->head == FENCEPOST_HEAD)); + assert(ok_address(m, p)); + assert(!is_small(sz)); + assert((len & (mparams.page_size-SIZE_T_ONE)) == 0); + assert(chunk_plus_offset(p, sz)->head == FENCEPOST_HEAD); + assert(chunk_plus_offset(p, sz+SIZE_T_SIZE)->head == 0); } /* Check properties of inuse chunks */ static void do_check_inuse_chunk(mstate m, mchunkptr p) { - do_check_any_chunk(m, p); - assert(is_inuse(p)); - assert(next_pinuse(p)); - /* If not pinuse and not mmapped, previous chunk has OK offset */ - assert(is_mmapped(p) || pinuse(p) || next_chunk(prev_chunk(p)) == p); - if (is_mmapped(p)) - do_check_mmapped_chunk(m, p); + do_check_any_chunk(m, p); + assert(is_inuse(p)); + assert(next_pinuse(p)); + /* If not pinuse and not mmapped, previous chunk has OK offset */ + assert(is_mmapped(p) || pinuse(p) || next_chunk(prev_chunk(p)) == p); + if (is_mmapped(p)) + do_check_mmapped_chunk(m, p); } /* Check properties of free chunks */ static void do_check_free_chunk(mstate m, mchunkptr p) { - size_t sz = chunksize(p); - mchunkptr next = chunk_plus_offset(p, sz); - do_check_any_chunk(m, p); - assert(!is_inuse(p)); - assert(!next_pinuse(p)); - assert (!is_mmapped(p)); - if (p != m->dv && p != m->top) { - if (sz >= MIN_CHUNK_SIZE) { - assert((sz & CHUNK_ALIGN_MASK) == 0); - assert(is_aligned(chunk2mem(p))); - assert(next->prev_foot == sz); - assert(pinuse(p)); - assert (next == m->top || is_inuse(next)); - assert(p->fd->bk == p); - assert(p->bk->fd == p); + size_t sz = chunksize(p); + mchunkptr next = chunk_plus_offset(p, sz); + do_check_any_chunk(m, p); + assert(!is_inuse(p)); + assert(!next_pinuse(p)); + assert (!is_mmapped(p)); + if (p != m->dv && p != m->top) { + if (sz >= MIN_CHUNK_SIZE) { + assert((sz & CHUNK_ALIGN_MASK) == 0); + assert(is_aligned(chunk2mem(p))); + assert(next->prev_foot == sz); + assert(pinuse(p)); + assert (next == m->top || is_inuse(next)); + assert(p->fd->bk == p); + assert(p->bk->fd == p); + } + else /* markers are always of size SIZE_T_SIZE */ + assert(sz == SIZE_T_SIZE); } - else /* markers are always of size SIZE_T_SIZE */ - assert(sz == SIZE_T_SIZE); - } } /* Check properties of malloced chunks at the point they are malloced */ static void do_check_malloced_chunk(mstate m, void* mem, size_t s) { - if (mem != 0) { - mchunkptr p = mem2chunk(mem); - size_t sz = p->head & ~INUSE_BITS; - do_check_inuse_chunk(m, p); - assert((sz & CHUNK_ALIGN_MASK) == 0); - assert(sz >= MIN_CHUNK_SIZE); - assert(sz >= s); - /* unless mmapped, size is less than MIN_CHUNK_SIZE more than request */ - assert(is_mmapped(p) || sz < (s + MIN_CHUNK_SIZE)); - } + if (mem != 0) { + mchunkptr p = mem2chunk(mem); + size_t sz = p->head & ~INUSE_BITS; + do_check_inuse_chunk(m, p); + assert((sz & CHUNK_ALIGN_MASK) == 0); + assert(sz >= MIN_CHUNK_SIZE); + assert(sz >= s); + /* unless mmapped, size is less than MIN_CHUNK_SIZE more than request */ + assert(is_mmapped(p) || sz < (s + MIN_CHUNK_SIZE)); + } } /* Check a tree and its subtrees. */ static void do_check_tree(mstate m, tchunkptr t) { - tchunkptr head = 0; - tchunkptr u = t; - bindex_t tindex = t->index; - size_t tsize = chunksize(t); - bindex_t idx; - compute_tree_index(tsize, idx); - assert(tindex == idx); - assert(tsize >= MIN_LARGE_SIZE); - assert(tsize >= minsize_for_tree_index(idx)); - assert((idx == NTREEBINS-1) || (tsize < minsize_for_tree_index((idx+1)))); - - do { /* traverse through chain of same-sized nodes */ - do_check_any_chunk(m, ((mchunkptr)u)); - assert(u->index == tindex); - assert(chunksize(u) == tsize); - assert(!is_inuse(u)); - assert(!next_pinuse(u)); - assert(u->fd->bk == u); - assert(u->bk->fd == u); - if (u->parent == 0) { - assert(u->child[0] == 0); - assert(u->child[1] == 0); - } - else { - assert(head == 0); /* only one node on chain has parent */ - head = u; - assert(u->parent != u); - assert (u->parent->child[0] == u || - u->parent->child[1] == u || - *((tbinptr*)(u->parent)) == u); - if (u->child[0] != 0) { - assert(u->child[0]->parent == u); - assert(u->child[0] != u); - do_check_tree(m, u->child[0]); - } - if (u->child[1] != 0) { - assert(u->child[1]->parent == u); - assert(u->child[1] != u); - do_check_tree(m, u->child[1]); - } - if (u->child[0] != 0 && u->child[1] != 0) { - assert(chunksize(u->child[0]) < chunksize(u->child[1])); - } - } - u = u->fd; - } while (u != t); - assert(head != 0); + tchunkptr head = 0; + tchunkptr u = t; + bindex_t tindex = t->index; + size_t tsize = chunksize(t); + bindex_t idx; + compute_tree_index(tsize, idx); + assert(tindex == idx); + assert(tsize >= MIN_LARGE_SIZE); + assert(tsize >= minsize_for_tree_index(idx)); + assert((idx == NTREEBINS-1) || (tsize < minsize_for_tree_index((idx+1)))); + + do { /* traverse through chain of same-sized nodes */ + do_check_any_chunk(m, ((mchunkptr)u)); + assert(u->index == tindex); + assert(chunksize(u) == tsize); + assert(!is_inuse(u)); + assert(!next_pinuse(u)); + assert(u->fd->bk == u); + assert(u->bk->fd == u); + if (u->parent == 0) { + assert(u->child[0] == 0); + assert(u->child[1] == 0); + } + else { + assert(head == 0); /* only one node on chain has parent */ + head = u; + assert(u->parent != u); + assert (u->parent->child[0] == u || + u->parent->child[1] == u || + *((tbinptr*)(u->parent)) == u); + if (u->child[0] != 0) { + assert(u->child[0]->parent == u); + assert(u->child[0] != u); + do_check_tree(m, u->child[0]); + } + if (u->child[1] != 0) { + assert(u->child[1]->parent == u); + assert(u->child[1] != u); + do_check_tree(m, u->child[1]); + } + if (u->child[0] != 0 && u->child[1] != 0) { + assert(chunksize(u->child[0]) < chunksize(u->child[1])); + } + } + u = u->fd; + } while (u != t); + assert(head != 0); } /* Check all the chunks in a treebin. */ static void do_check_treebin(mstate m, bindex_t i) { - tbinptr* tb = treebin_at(m, i); - tchunkptr t = *tb; - int empty = (m->treemap & (1U << i)) == 0; - if (t == 0) - assert(empty); - if (!empty) - do_check_tree(m, t); + tbinptr* tb = treebin_at(m, i); + tchunkptr t = *tb; + int empty = (m->treemap & (1U << i)) == 0; + if (t == 0) + assert(empty); + if (!empty) + do_check_tree(m, t); } /* Check all the chunks in a smallbin. */ static void do_check_smallbin(mstate m, bindex_t i) { - sbinptr b = smallbin_at(m, i); - mchunkptr p = b->bk; - unsigned int empty = (m->smallmap & (1U << i)) == 0; - if (p == b) - assert(empty); - if (!empty) { - for (; p != b; p = p->bk) { - size_t size = chunksize(p); - mchunkptr q; - /* each chunk claims to be free */ - do_check_free_chunk(m, p); - /* chunk belongs in bin */ - assert(small_index(size) == i); - assert(p->bk == b || chunksize(p->bk) == chunksize(p)); - /* chunk is followed by an inuse chunk */ - q = next_chunk(p); - if (q->head != FENCEPOST_HEAD) - do_check_inuse_chunk(m, q); + sbinptr b = smallbin_at(m, i); + mchunkptr p = b->bk; + unsigned int empty = (m->smallmap & (1U << i)) == 0; + if (p == b) + assert(empty); + if (!empty) { + for (; p != b; p = p->bk) { + size_t size = chunksize(p); + mchunkptr q; + /* each chunk claims to be free */ + do_check_free_chunk(m, p); + /* chunk belongs in bin */ + assert(small_index(size) == i); + assert(p->bk == b || chunksize(p->bk) == chunksize(p)); + /* chunk is followed by an inuse chunk */ + q = next_chunk(p); + if (q->head != FENCEPOST_HEAD) + do_check_inuse_chunk(m, q); + } } - } } /* Find x in a bin. Used in other check functions. */ static int bin_find(mstate m, mchunkptr x) { - size_t size = chunksize(x); - if (is_small(size)) { - bindex_t sidx = small_index(size); - sbinptr b = smallbin_at(m, sidx); - if (smallmap_is_marked(m, sidx)) { - mchunkptr p = b; - do { - if (p == x) - return 1; - } while ((p = p->fd) != b); + size_t size = chunksize(x); + if (is_small(size)) { + bindex_t sidx = small_index(size); + sbinptr b = smallbin_at(m, sidx); + if (smallmap_is_marked(m, sidx)) { + mchunkptr p = b; + do { + if (p == x) + return 1; + } while ((p = p->fd) != b); + } } - } - else { - bindex_t tidx; - compute_tree_index(size, tidx); - if (treemap_is_marked(m, tidx)) { - tchunkptr t = *treebin_at(m, tidx); - size_t sizebits = size << leftshift_for_tree_index(tidx); - while (t != 0 && chunksize(t) != size) { - t = t->child[(sizebits >> (SIZE_T_BITSIZE-SIZE_T_ONE)) & 1]; - sizebits <<= 1; - } - if (t != 0) { - tchunkptr u = t; - do { - if (u == (tchunkptr)x) - return 1; - } while ((u = u->fd) != t); - } + else { + bindex_t tidx; + compute_tree_index(size, tidx); + if (treemap_is_marked(m, tidx)) { + tchunkptr t = *treebin_at(m, tidx); + size_t sizebits = size << leftshift_for_tree_index(tidx); + while (t != 0 && chunksize(t) != size) { + t = t->child[(sizebits >> (SIZE_T_BITSIZE-SIZE_T_ONE)) & 1]; + sizebits <<= 1; + } + if (t != 0) { + tchunkptr u = t; + do { + if (u == (tchunkptr)x) + return 1; + } while ((u = u->fd) != t); + } + } } - } - return 0; + return 0; } /* Traverse each chunk and check it; return total */ static size_t traverse_and_check(mstate m) { - size_t sum = 0; - if (is_initialized(m)) { - msegmentptr s = &m->seg; - sum += m->topsize + TOP_FOOT_SIZE; - while (s != 0) { - mchunkptr q = align_as_chunk(s->base); - mchunkptr lastq = 0; - assert(pinuse(q)); - while (segment_holds(s, q) && - q != m->top && q->head != FENCEPOST_HEAD) { - sum += chunksize(q); - if (is_inuse(q)) { - assert(!bin_find(m, q)); - do_check_inuse_chunk(m, q); - } - else { - assert(q == m->dv || bin_find(m, q)); - assert(lastq == 0 || is_inuse(lastq)); /* Not 2 consecutive free */ - do_check_free_chunk(m, q); + size_t sum = 0; + if (is_initialized(m)) { + msegmentptr s = &m->seg; + sum += m->topsize + TOP_FOOT_SIZE; + while (s != 0) { + mchunkptr q = align_as_chunk(s->base); + mchunkptr lastq = 0; + assert(pinuse(q)); + while (segment_holds(s, q) && + q != m->top && q->head != FENCEPOST_HEAD) { + sum += chunksize(q); + if (is_inuse(q)) { + assert(!bin_find(m, q)); + do_check_inuse_chunk(m, q); + } + else { + assert(q == m->dv || bin_find(m, q)); + assert(lastq == 0 || is_inuse(lastq)); /* Not 2 consecutive free */ + do_check_free_chunk(m, q); + } + lastq = q; + q = next_chunk(q); + } + s = s->next; } - lastq = q; - q = next_chunk(q); - } - s = s->next; } - } - return sum; + return sum; } + /* Check all properties of malloc_state. */ static void do_check_malloc_state(mstate m) { - bindex_t i; - size_t total; - /* check bins */ - for (i = 0; i < NSMALLBINS; ++i) - do_check_smallbin(m, i); - for (i = 0; i < NTREEBINS; ++i) - do_check_treebin(m, i); - - if (m->dvsize != 0) { /* check dv chunk */ - do_check_any_chunk(m, m->dv); - assert(m->dvsize == chunksize(m->dv)); - assert(m->dvsize >= MIN_CHUNK_SIZE); - assert(bin_find(m, m->dv) == 0); - } - - if (m->top != 0) { /* check top chunk */ - do_check_top_chunk(m, m->top); - /*assert(m->topsize == chunksize(m->top)); redundant */ - assert(m->topsize > 0); - assert(bin_find(m, m->top) == 0); - } - - total = traverse_and_check(m); - assert(total <= m->footprint); - assert(m->footprint <= m->max_footprint); + bindex_t i; + size_t total; + /* check bins */ + for (i = 0; i < NSMALLBINS; ++i) + do_check_smallbin(m, i); + for (i = 0; i < NTREEBINS; ++i) + do_check_treebin(m, i); + + if (m->dvsize != 0) { /* check dv chunk */ + do_check_any_chunk(m, m->dv); + assert(m->dvsize == chunksize(m->dv)); + assert(m->dvsize >= MIN_CHUNK_SIZE); + assert(bin_find(m, m->dv) == 0); + } + + if (m->top != 0) { /* check top chunk */ + do_check_top_chunk(m, m->top); + /*assert(m->topsize == chunksize(m->top)); redundant */ + assert(m->topsize > 0); + assert(bin_find(m, m->top) == 0); + } + + total = traverse_and_check(m); + assert(total <= m->footprint); + assert(m->footprint <= m->max_footprint); } #endif /* DEBUG */ @@ -3335,309 +3494,314 @@ static void do_check_malloc_state(mstate m) { #if !NO_MALLINFO static struct mallinfo internal_mallinfo(mstate m) { - struct mallinfo nm = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; - ensure_initialization(); - if (!PREACTION(m)) { - check_malloc_state(m); - if (is_initialized(m)) { - size_t nfree = SIZE_T_ONE; /* top always free */ - size_t mfree = m->topsize + TOP_FOOT_SIZE; - size_t sum = mfree; - msegmentptr s = &m->seg; - while (s != 0) { - mchunkptr q = align_as_chunk(s->base); - while (segment_holds(s, q) && - q != m->top && q->head != FENCEPOST_HEAD) { - size_t sz = chunksize(q); - sum += sz; - if (!is_inuse(q)) { - mfree += sz; - ++nfree; - } - q = next_chunk(q); + struct mallinfo nm = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + ensure_initialization(); + if (!PREACTION(m)) { + check_malloc_state(m); + if (is_initialized(m)) { + size_t nfree = SIZE_T_ONE; /* top always free */ + size_t mfree = m->topsize + TOP_FOOT_SIZE; + size_t sum = mfree; + msegmentptr s = &m->seg; + while (s != 0) { + mchunkptr q = align_as_chunk(s->base); + while (segment_holds(s, q) && + q != m->top && q->head != FENCEPOST_HEAD) { + size_t sz = chunksize(q); + sum += sz; + if (!is_inuse(q)) { + mfree += sz; + ++nfree; + } + q = next_chunk(q); + } + s = s->next; + } + + nm.arena = sum; + nm.ordblks = nfree; + nm.hblkhd = m->footprint - sum; + nm.usmblks = m->max_footprint; + nm.uordblks = m->footprint - mfree; + nm.fordblks = mfree; + nm.keepcost = m->topsize; } - s = s->next; - } - - nm.arena = sum; - nm.ordblks = nfree; - nm.hblkhd = m->footprint - sum; - nm.usmblks = m->max_footprint; - nm.uordblks = m->footprint - mfree; - nm.fordblks = mfree; - nm.keepcost = m->topsize; + + POSTACTION(m); } - - POSTACTION(m); - } - return nm; + return nm; } #endif /* !NO_MALLINFO */ +#if !NO_MALLOC_STATS static void internal_malloc_stats(mstate m) { - ensure_initialization(); - if (!PREACTION(m)) { - size_t maxfp = 0; - size_t fp = 0; - size_t used = 0; - check_malloc_state(m); - if (is_initialized(m)) { - msegmentptr s = &m->seg; - maxfp = m->max_footprint; - fp = m->footprint; - used = fp - (m->topsize + TOP_FOOT_SIZE); - - while (s != 0) { - mchunkptr q = align_as_chunk(s->base); - while (segment_holds(s, q) && - q != m->top && q->head != FENCEPOST_HEAD) { - if (!is_inuse(q)) - used -= chunksize(q); - q = next_chunk(q); + ensure_initialization(); + if (!PREACTION(m)) { + size_t maxfp = 0; + size_t fp = 0; + size_t used = 0; + check_malloc_state(m); + if (is_initialized(m)) { + msegmentptr s = &m->seg; + maxfp = m->max_footprint; + fp = m->footprint; + used = fp - (m->topsize + TOP_FOOT_SIZE); + + while (s != 0) { + mchunkptr q = align_as_chunk(s->base); + while (segment_holds(s, q) && + q != m->top && q->head != FENCEPOST_HEAD) { + if (!is_inuse(q)) + used -= chunksize(q); + q = next_chunk(q); + } + s = s->next; + } } - s = s->next; - } + POSTACTION(m); /* drop lock */ + fprintf(stderr, "max system bytes = %10lu\n", (unsigned long)(maxfp)); + fprintf(stderr, "system bytes = %10lu\n", (unsigned long)(fp)); + fprintf(stderr, "in use bytes = %10lu\n", (unsigned long)(used)); } - - fprintf(stderr, "max system bytes = %10lu\n", (unsigned long)(maxfp)); - fprintf(stderr, "system bytes = %10lu\n", (unsigned long)(fp)); - fprintf(stderr, "in use bytes = %10lu\n", (unsigned long)(used)); - - POSTACTION(m); - } } +#endif /* NO_MALLOC_STATS */ /* ----------------------- Operations on smallbins ----------------------- */ /* - Various forms of linking and unlinking are defined as macros. Even - the ones for trees, which are very long but have very short typical - paths. This is ugly but reduces reliance on inlining support of - compilers. -*/ + Various forms of linking and unlinking are defined as macros. Even + the ones for trees, which are very long but have very short typical + paths. This is ugly but reduces reliance on inlining support of + compilers. + */ /* Link a free chunk into a smallbin */ #define insert_small_chunk(M, P, S) {\ - bindex_t I = small_index(S);\ - mchunkptr B = smallbin_at(M, I);\ - mchunkptr F = B;\ - assert(S >= MIN_CHUNK_SIZE);\ - if (!smallmap_is_marked(M, I))\ - mark_smallmap(M, I);\ - else if (RTCHECK(ok_address(M, B->fd)))\ - F = B->fd;\ - else {\ - CORRUPTION_ERROR_ACTION(M);\ - }\ - B->fd = P;\ - F->bk = P;\ - P->fd = F;\ - P->bk = B;\ +bindex_t I = small_index(S);\ +mchunkptr B = smallbin_at(M, I);\ +mchunkptr F = B;\ +assert(S >= MIN_CHUNK_SIZE);\ +if (!smallmap_is_marked(M, I))\ +mark_smallmap(M, I);\ +else if (RTCHECK(ok_address(M, B->fd)))\ +F = B->fd;\ +else {\ +CORRUPTION_ERROR_ACTION(M);\ +}\ +B->fd = P;\ +F->bk = P;\ +P->fd = F;\ +P->bk = B;\ } /* Unlink a chunk from a smallbin */ #define unlink_small_chunk(M, P, S) {\ - mchunkptr F = P->fd;\ - mchunkptr B = P->bk;\ - bindex_t I = small_index(S);\ - assert(P != B);\ - assert(P != F);\ - assert(chunksize(P) == small_index2size(I));\ - if (F == B)\ - clear_smallmap(M, I);\ - else if (RTCHECK((F == smallbin_at(M,I) || ok_address(M, F)) &&\ - (B == smallbin_at(M,I) || ok_address(M, B)))) {\ - F->bk = B;\ - B->fd = F;\ - }\ - else {\ - CORRUPTION_ERROR_ACTION(M);\ - }\ +mchunkptr F = P->fd;\ +mchunkptr B = P->bk;\ +bindex_t I = small_index(S);\ +assert(P != B);\ +assert(P != F);\ +assert(chunksize(P) == small_index2size(I));\ +if (RTCHECK(F == smallbin_at(M,I) || (ok_address(M, F) && F->bk == P))) { \ +if (B == F) {\ +clear_smallmap(M, I);\ +}\ +else if (RTCHECK(B == smallbin_at(M,I) ||\ +(ok_address(M, B) && B->fd == P))) {\ +F->bk = B;\ +B->fd = F;\ +}\ +else {\ +CORRUPTION_ERROR_ACTION(M);\ +}\ +}\ +else {\ +CORRUPTION_ERROR_ACTION(M);\ +}\ } /* Unlink the first chunk from a smallbin */ #define unlink_first_small_chunk(M, B, P, I) {\ - mchunkptr F = P->fd;\ - assert(P != B);\ - assert(P != F);\ - assert(chunksize(P) == small_index2size(I));\ - if (B == F)\ - clear_smallmap(M, I);\ - else if (RTCHECK(ok_address(M, F))) {\ - B->fd = F;\ - F->bk = B;\ - }\ - else {\ - CORRUPTION_ERROR_ACTION(M);\ - }\ +mchunkptr F = P->fd;\ +assert(P != B);\ +assert(P != F);\ +assert(chunksize(P) == small_index2size(I));\ +if (B == F) {\ +clear_smallmap(M, I);\ +}\ +else if (RTCHECK(ok_address(M, F) && F->bk == P)) {\ +F->bk = B;\ +B->fd = F;\ +}\ +else {\ +CORRUPTION_ERROR_ACTION(M);\ +}\ } - - /* Replace dv node, binning the old one */ /* Used only when dvsize known to be small */ #define replace_dv(M, P, S) {\ - size_t DVS = M->dvsize;\ - if (DVS != 0) {\ - mchunkptr DV = M->dv;\ - assert(is_small(DVS));\ - insert_small_chunk(M, DV, DVS);\ - }\ - M->dvsize = S;\ - M->dv = P;\ +size_t DVS = M->dvsize;\ +assert(is_small(DVS));\ +if (DVS != 0) {\ +mchunkptr DV = M->dv;\ +insert_small_chunk(M, DV, DVS);\ +}\ +M->dvsize = S;\ +M->dv = P;\ } /* ------------------------- Operations on trees ------------------------- */ /* Insert chunk into tree */ #define insert_large_chunk(M, X, S) {\ - tbinptr* H;\ - bindex_t I;\ - compute_tree_index(S, I);\ - H = treebin_at(M, I);\ - X->index = I;\ - X->child[0] = X->child[1] = 0;\ - if (!treemap_is_marked(M, I)) {\ - mark_treemap(M, I);\ - *H = X;\ - X->parent = (tchunkptr)H;\ - X->fd = X->bk = X;\ - }\ - else {\ - tchunkptr T = *H;\ - size_t K = S << leftshift_for_tree_index(I);\ - for (;;) {\ - if (chunksize(T) != S) {\ - tchunkptr* C = &(T->child[(K >> (SIZE_T_BITSIZE-SIZE_T_ONE)) & 1]);\ - K <<= 1;\ - if (*C != 0)\ - T = *C;\ - else if (RTCHECK(ok_address(M, C))) {\ - *C = X;\ - X->parent = T;\ - X->fd = X->bk = X;\ - break;\ - }\ - else {\ - CORRUPTION_ERROR_ACTION(M);\ - break;\ - }\ - }\ - else {\ - tchunkptr F = T->fd;\ - if (RTCHECK(ok_address(M, T) && ok_address(M, F))) {\ - T->fd = F->bk = X;\ - X->fd = F;\ - X->bk = T;\ - X->parent = 0;\ - break;\ - }\ - else {\ - CORRUPTION_ERROR_ACTION(M);\ - break;\ - }\ - }\ - }\ - }\ +tbinptr* H;\ +bindex_t I;\ +compute_tree_index(S, I);\ +H = treebin_at(M, I);\ +X->index = I;\ +X->child[0] = X->child[1] = 0;\ +if (!treemap_is_marked(M, I)) {\ +mark_treemap(M, I);\ +*H = X;\ +X->parent = (tchunkptr)H;\ +X->fd = X->bk = X;\ +}\ +else {\ +tchunkptr T = *H;\ +size_t K = S << leftshift_for_tree_index(I);\ +for (;;) {\ +if (chunksize(T) != S) {\ +tchunkptr* C = &(T->child[(K >> (SIZE_T_BITSIZE-SIZE_T_ONE)) & 1]);\ +K <<= 1;\ +if (*C != 0)\ +T = *C;\ +else if (RTCHECK(ok_address(M, C))) {\ +*C = X;\ +X->parent = T;\ +X->fd = X->bk = X;\ +break;\ +}\ +else {\ +CORRUPTION_ERROR_ACTION(M);\ +break;\ +}\ +}\ +else {\ +tchunkptr F = T->fd;\ +if (RTCHECK(ok_address(M, T) && ok_address(M, F))) {\ +T->fd = F->bk = X;\ +X->fd = F;\ +X->bk = T;\ +X->parent = 0;\ +break;\ +}\ +else {\ +CORRUPTION_ERROR_ACTION(M);\ +break;\ +}\ +}\ +}\ +}\ } /* - Unlink steps: - - 1. If x is a chained node, unlink it from its same-sized fd/bk links - and choose its bk node as its replacement. - 2. If x was the last node of its size, but not a leaf node, it must - be replaced with a leaf node (not merely one with an open left or - right), to make sure that lefts and rights of descendents - correspond properly to bit masks. We use the rightmost descendent - of x. We could use any other leaf, but this is easy to locate and - tends to counteract removal of leftmosts elsewhere, and so keeps - paths shorter than minimally guaranteed. This doesn't loop much - because on average a node in a tree is near the bottom. - 3. If x is the base of a chain (i.e., has parent links) relink - x's parent and children to x's replacement (or null if none). -*/ - -#define unlink_large_chunk(M, X) {\ - tchunkptr XP = X->parent;\ - tchunkptr R;\ - if (X->bk != X) {\ - tchunkptr F = X->fd;\ - R = X->bk;\ - if (RTCHECK(ok_address(M, F))) {\ - F->bk = R;\ - R->fd = F;\ - }\ - else {\ - CORRUPTION_ERROR_ACTION(M);\ - }\ - }\ - else {\ - tchunkptr* RP;\ - if (((R = *(RP = &(X->child[1]))) != 0) ||\ - ((R = *(RP = &(X->child[0]))) != 0)) {\ - tchunkptr* CP;\ - while ((*(CP = &(R->child[1])) != 0) ||\ - (*(CP = &(R->child[0])) != 0)) {\ - R = *(RP = CP);\ - }\ - if (RTCHECK(ok_address(M, RP)))\ - *RP = 0;\ - else {\ - CORRUPTION_ERROR_ACTION(M);\ - }\ - }\ - }\ - if (XP != 0) {\ - tbinptr* H = treebin_at(M, X->index);\ - if (X == *H) {\ - if ((*H = R) == 0) \ - clear_treemap(M, X->index);\ - }\ - else if (RTCHECK(ok_address(M, XP))) {\ - if (XP->child[0] == X) \ - XP->child[0] = R;\ - else \ - XP->child[1] = R;\ - }\ - else\ - CORRUPTION_ERROR_ACTION(M);\ - if (R != 0) {\ - if (RTCHECK(ok_address(M, R))) {\ - tchunkptr C0, C1;\ - R->parent = XP;\ - if ((C0 = X->child[0]) != 0) {\ - if (RTCHECK(ok_address(M, C0))) {\ - R->child[0] = C0;\ - C0->parent = R;\ - }\ - else\ - CORRUPTION_ERROR_ACTION(M);\ - }\ - if ((C1 = X->child[1]) != 0) {\ - if (RTCHECK(ok_address(M, C1))) {\ - R->child[1] = C1;\ - C1->parent = R;\ - }\ - else\ - CORRUPTION_ERROR_ACTION(M);\ - }\ - }\ - else\ - CORRUPTION_ERROR_ACTION(M);\ - }\ - }\ + Unlink steps: + + 1. If x is a chained node, unlink it from its same-sized fd/bk links + and choose its bk node as its replacement. + 2. If x was the last node of its size, but not a leaf node, it must + be replaced with a leaf node (not merely one with an open left or + right), to make sure that lefts and rights of descendents + correspond properly to bit masks. We use the rightmost descendent + of x. We could use any other leaf, but this is easy to locate and + tends to counteract removal of leftmosts elsewhere, and so keeps + paths shorter than minimally guaranteed. This doesn't loop much + because on average a node in a tree is near the bottom. + 3. If x is the base of a chain (i.e., has parent links) relink + x's parent and children to x's replacement (or null if none). + */ + +#define unlink_large_chunk(M, X) { \ +tchunkptr XP = X->parent; \ +tchunkptr R; \ +if (X->bk != X) { \ +tchunkptr F = X->fd; \ +R = X->bk; \ +if (RTCHECK(ok_address(M, F) && F->bk == X && R->fd == X)) { \ +F->bk = R; \ +R->fd = F; \ +} \ +else { \ +CORRUPTION_ERROR_ACTION(M); \ +} \ +} \ +else { \ +tchunkptr* RP; \ +if (((R = *(RP = &(X->child[1]))) != 0) || \ +((R = *(RP = &(X->child[0]))) != 0)) { \ +tchunkptr* CP; \ +while ((*(CP = &(R->child[1])) != 0) || \ +(*(CP = &(R->child[0])) != 0)) { \ +R = *(RP = CP); \ +} \ +if (RTCHECK(ok_address(M, RP))) \ +*RP = 0; \ +else { \ +CORRUPTION_ERROR_ACTION(M); \ +} \ +} \ +} \ +if (XP != 0) { \ +tbinptr* H = treebin_at(M, X->index); \ +if (X == *H) { \ +if ((*H = R) == 0) \ +clear_treemap(M, X->index); \ +} \ +else if (RTCHECK(ok_address(M, XP))) { \ +if (XP->child[0] == X) \ +XP->child[0] = R; \ +else \ +XP->child[1] = R; \ +} \ +else \ +CORRUPTION_ERROR_ACTION(M); \ +if (R != 0) { \ +if (RTCHECK(ok_address(M, R))) { \ +tchunkptr C0, C1; \ +R->parent = XP; \ +if ((C0 = X->child[0]) != 0) { \ +if (RTCHECK(ok_address(M, C0))) { \ +R->child[0] = C0; \ +C0->parent = R; \ +} \ +else \ +CORRUPTION_ERROR_ACTION(M); \ +} \ +if ((C1 = X->child[1]) != 0) { \ +if (RTCHECK(ok_address(M, C1))) { \ +R->child[1] = C1; \ +C1->parent = R; \ +} \ +else \ +CORRUPTION_ERROR_ACTION(M); \ +} \ +} \ +else \ +CORRUPTION_ERROR_ACTION(M); \ +} \ +} \ } /* Relays to large vs small bin operations */ -#define insert_chunk(M, P, S)\ - if (is_small(S)) insert_small_chunk(M, P, S)\ - else { tchunkptr TP = (tchunkptr)(P); insert_large_chunk(M, TP, S); } +#define insert_chunk(M, P, S) \ +if (is_small(S)) insert_small_chunk(M, P, S) \ +else { tchunkptr TP = (tchunkptr)(P); insert_large_chunk(M, TP, S); } -#define unlink_chunk(M, P, S)\ - if (is_small(S)) unlink_small_chunk(M, P, S)\ - else { tchunkptr TP = (tchunkptr)(P); unlink_large_chunk(M, TP); } +#define unlink_chunk(M, P, S) \ +if (is_small(S)) unlink_small_chunk(M, P, S) \ +else { tchunkptr TP = (tchunkptr)(P); unlink_large_chunk(M, TP); } /* Relays to internal calls to malloc/free from realloc, memalign etc */ @@ -3648,9 +3812,9 @@ static void internal_malloc_stats(mstate m) { #else /* ONLY_MSPACES */ #if MSPACES #define internal_malloc(m, b)\ - (m == gm)? dlmalloc(b) : mspace_malloc(m, b) +((m == gm)? dlmalloc(b) : mspace_malloc(m, b)) #define internal_free(m, mem)\ - if (m == gm) dlfree(mem); else mspace_free(m,mem); +if (m == gm) dlfree(mem); else mspace_free(m,mem); #else /* MSPACES */ #define internal_malloc(m, b) dlmalloc(b) #define internal_free(m, mem) dlfree(mem) @@ -3660,1759 +3824,2159 @@ static void internal_malloc_stats(mstate m) { /* ----------------------- Direct-mmapping chunks ----------------------- */ /* - Directly mmapped chunks are set up with an offset to the start of - the mmapped region stored in the prev_foot field of the chunk. This - allows reconstruction of the required argument to MUNMAP when freed, - and also allows adjustment of the returned chunk to meet alignment - requirements (especially in memalign). -*/ + Directly mmapped chunks are set up with an offset to the start of + the mmapped region stored in the prev_foot field of the chunk. This + allows reconstruction of the required argument to MUNMAP when freed, + and also allows adjustment of the returned chunk to meet alignment + requirements (especially in memalign). + */ /* Malloc using mmap */ static void* mmap_alloc(mstate m, size_t nb) { - size_t mmsize = mmap_align(nb + SIX_SIZE_T_SIZES + CHUNK_ALIGN_MASK); - if (mmsize > nb) { /* Check for wrap around 0 */ - char* mm = (char*)(CALL_DIRECT_MMAP(mmsize)); - if (mm != CMFAIL) { - size_t offset = align_offset(chunk2mem(mm)); - size_t psize = mmsize - offset - MMAP_FOOT_PAD; - mchunkptr p = (mchunkptr)(mm + offset); - p->prev_foot = offset; - p->head = psize; - mark_inuse_foot(m, p, psize); - chunk_plus_offset(p, psize)->head = FENCEPOST_HEAD; - chunk_plus_offset(p, psize+SIZE_T_SIZE)->head = 0; - - if (m->least_addr == 0 || mm < m->least_addr) - m->least_addr = mm; - if ((m->footprint += mmsize) > m->max_footprint) - m->max_footprint = m->footprint; - assert(is_aligned(chunk2mem(p))); - check_mmapped_chunk(m, p); - return chunk2mem(p); + size_t mmsize = mmap_align(nb + SIX_SIZE_T_SIZES + CHUNK_ALIGN_MASK); + if (m->footprint_limit != 0) { + size_t fp = m->footprint + mmsize; + if (fp <= m->footprint || fp > m->footprint_limit) + return 0; } - } - return 0; + if (mmsize > nb) { /* Check for wrap around 0 */ + char* mm = (char*)(CALL_DIRECT_MMAP(mmsize)); + if (mm != CMFAIL) { + size_t offset = align_offset(chunk2mem(mm)); + size_t psize = mmsize - offset - MMAP_FOOT_PAD; + mchunkptr p = (mchunkptr)(mm + offset); + p->prev_foot = offset; + p->head = psize; + mark_inuse_foot(m, p, psize); + chunk_plus_offset(p, psize)->head = FENCEPOST_HEAD; + chunk_plus_offset(p, psize+SIZE_T_SIZE)->head = 0; + + if (m->least_addr == 0 || mm < m->least_addr) + m->least_addr = mm; + if ((m->footprint += mmsize) > m->max_footprint) + m->max_footprint = m->footprint; + assert(is_aligned(chunk2mem(p))); + check_mmapped_chunk(m, p); + return chunk2mem(p); + } + } + return 0; } /* Realloc using mmap */ -static mchunkptr mmap_resize(mstate m, mchunkptr oldp, size_t nb) { - size_t oldsize = chunksize(oldp); - if (is_small(nb)) /* Can't shrink mmap regions below small size */ - return 0; - /* Keep old chunk if big enough but not too big */ - if (oldsize >= nb + SIZE_T_SIZE && - (oldsize - nb) <= (mparams.granularity << 1)) - return oldp; - else { - size_t offset = oldp->prev_foot; - size_t oldmmsize = oldsize + offset + MMAP_FOOT_PAD; - size_t newmmsize = mmap_align(nb + SIX_SIZE_T_SIZES + CHUNK_ALIGN_MASK); - char* cp = (char*)CALL_MREMAP((char*)oldp - offset, - oldmmsize, newmmsize, 1); - if (cp != CMFAIL) { - mchunkptr newp = (mchunkptr)(cp + offset); - size_t psize = newmmsize - offset - MMAP_FOOT_PAD; - newp->head = psize; - mark_inuse_foot(m, newp, psize); - chunk_plus_offset(newp, psize)->head = FENCEPOST_HEAD; - chunk_plus_offset(newp, psize+SIZE_T_SIZE)->head = 0; - - if (cp < m->least_addr) - m->least_addr = cp; - if ((m->footprint += newmmsize - oldmmsize) > m->max_footprint) - m->max_footprint = m->footprint; - check_mmapped_chunk(m, newp); - return newp; +static mchunkptr mmap_resize(mstate m, mchunkptr oldp, size_t nb, int flags) { + size_t oldsize = chunksize(oldp); + (void)flags; /* placate people compiling -Wunused */ + if (is_small(nb)) /* Can't shrink mmap regions below small size */ + return 0; + /* Keep old chunk if big enough but not too big */ + if (oldsize >= nb + SIZE_T_SIZE && + (oldsize - nb) <= (mparams.granularity << 1)) + return oldp; + else { + size_t offset = oldp->prev_foot; + size_t oldmmsize = oldsize + offset + MMAP_FOOT_PAD; + size_t newmmsize = mmap_align(nb + SIX_SIZE_T_SIZES + CHUNK_ALIGN_MASK); + char* cp = (char*)CALL_MREMAP((char*)oldp - offset, + oldmmsize, newmmsize, flags); + if (cp != CMFAIL) { + mchunkptr newp = (mchunkptr)(cp + offset); + size_t psize = newmmsize - offset - MMAP_FOOT_PAD; + newp->head = psize; + mark_inuse_foot(m, newp, psize); + chunk_plus_offset(newp, psize)->head = FENCEPOST_HEAD; + chunk_plus_offset(newp, psize+SIZE_T_SIZE)->head = 0; + + if (cp < m->least_addr) + m->least_addr = cp; + if ((m->footprint += newmmsize - oldmmsize) > m->max_footprint) + m->max_footprint = m->footprint; + check_mmapped_chunk(m, newp); + return newp; + } } - } - return 0; + return 0; } + /* -------------------------- mspace management -------------------------- */ /* Initialize top chunk and its size */ static void init_top(mstate m, mchunkptr p, size_t psize) { - /* Ensure alignment */ - size_t offset = align_offset(chunk2mem(p)); - p = (mchunkptr)((char*)p + offset); - psize -= offset; - - m->top = p; - m->topsize = psize; - p->head = psize | PINUSE_BIT; - /* set size of fake trailing chunk holding overhead space only once */ - chunk_plus_offset(p, psize)->head = TOP_FOOT_SIZE; - m->trim_check = mparams.trim_threshold; /* reset on each update */ + /* Ensure alignment */ + size_t offset = align_offset(chunk2mem(p)); + p = (mchunkptr)((char*)p + offset); + psize -= offset; + + m->top = p; + m->topsize = psize; + p->head = psize | PINUSE_BIT; + /* set size of fake trailing chunk holding overhead space only once */ + chunk_plus_offset(p, psize)->head = TOP_FOOT_SIZE; + m->trim_check = mparams.trim_threshold; /* reset on each update */ } /* Initialize bins for a new mstate that is otherwise zeroed out */ static void init_bins(mstate m) { - /* Establish circular links for smallbins */ - bindex_t i; - for (i = 0; i < NSMALLBINS; ++i) { - sbinptr bin = smallbin_at(m,i); - bin->fd = bin->bk = bin; - } + /* Establish circular links for smallbins */ + bindex_t i; + for (i = 0; i < NSMALLBINS; ++i) { + sbinptr bin = smallbin_at(m,i); + bin->fd = bin->bk = bin; + } } #if PROCEED_ON_ERROR /* default corruption action */ static void reset_on_error(mstate m) { - int i; - ++malloc_corruption_error_count; - /* Reinitialize fields to forget about all memory */ - m->smallbins = m->treebins = 0; - m->dvsize = m->topsize = 0; - m->seg.base = 0; - m->seg.size = 0; - m->seg.next = 0; - m->top = m->dv = 0; - for (i = 0; i < NTREEBINS; ++i) - *treebin_at(m, i) = 0; - init_bins(m); + int i; + ++malloc_corruption_error_count; + /* Reinitialize fields to forget about all memory */ + m->smallmap = m->treemap = 0; + m->dvsize = m->topsize = 0; + m->seg.base = 0; + m->seg.size = 0; + m->seg.next = 0; + m->top = m->dv = 0; + for (i = 0; i < NTREEBINS; ++i) + *treebin_at(m, i) = 0; + init_bins(m); } #endif /* PROCEED_ON_ERROR */ /* Allocate chunk and prepend remainder with chunk in successor base. */ static void* prepend_alloc(mstate m, char* newbase, char* oldbase, size_t nb) { - mchunkptr p = align_as_chunk(newbase); - mchunkptr oldfirst = align_as_chunk(oldbase); - size_t psize = (char*)oldfirst - (char*)p; - mchunkptr q = chunk_plus_offset(p, nb); - size_t qsize = psize - nb; - set_size_and_pinuse_of_inuse_chunk(m, p, nb); - - assert((char*)oldfirst > (char*)q); - assert(pinuse(oldfirst)); - assert(qsize >= MIN_CHUNK_SIZE); - - /* consolidate remainder with first chunk of old base */ - if (oldfirst == m->top) { - size_t tsize = m->topsize += qsize; - m->top = q; - q->head = tsize | PINUSE_BIT; - check_top_chunk(m, q); - } - else if (oldfirst == m->dv) { - size_t dsize = m->dvsize += qsize; - m->dv = q; - set_size_and_pinuse_of_free_chunk(q, dsize); - } - else { - if (!is_inuse(oldfirst)) { - size_t nsize = chunksize(oldfirst); - unlink_chunk(m, oldfirst, nsize); - oldfirst = chunk_plus_offset(oldfirst, nsize); - qsize += nsize; + mchunkptr p = align_as_chunk(newbase); + mchunkptr oldfirst = align_as_chunk(oldbase); + size_t psize = (char*)oldfirst - (char*)p; + mchunkptr q = chunk_plus_offset(p, nb); + size_t qsize = psize - nb; + set_size_and_pinuse_of_inuse_chunk(m, p, nb); + + assert((char*)oldfirst > (char*)q); + assert(pinuse(oldfirst)); + assert(qsize >= MIN_CHUNK_SIZE); + + /* consolidate remainder with first chunk of old base */ + if (oldfirst == m->top) { + size_t tsize = m->topsize += qsize; + m->top = q; + q->head = tsize | PINUSE_BIT; + check_top_chunk(m, q); } - set_free_with_pinuse(q, qsize, oldfirst); - insert_chunk(m, q, qsize); - check_free_chunk(m, q); - } - - check_malloced_chunk(m, chunk2mem(p), nb); - return chunk2mem(p); + else if (oldfirst == m->dv) { + size_t dsize = m->dvsize += qsize; + m->dv = q; + set_size_and_pinuse_of_free_chunk(q, dsize); + } + else { + if (!is_inuse(oldfirst)) { + size_t nsize = chunksize(oldfirst); + unlink_chunk(m, oldfirst, nsize); + oldfirst = chunk_plus_offset(oldfirst, nsize); + qsize += nsize; + } + set_free_with_pinuse(q, qsize, oldfirst); + insert_chunk(m, q, qsize); + check_free_chunk(m, q); + } + + check_malloced_chunk(m, chunk2mem(p), nb); + return chunk2mem(p); } /* Add a segment to hold a new noncontiguous region */ static void add_segment(mstate m, char* tbase, size_t tsize, flag_t mmapped) { - /* Determine locations and sizes of segment, fenceposts, old top */ - char* old_top = (char*)m->top; - msegmentptr oldsp = segment_holding(m, old_top); - char* old_end = oldsp->base + oldsp->size; - size_t ssize = pad_request(sizeof(struct malloc_segment)); - char* rawsp = old_end - (ssize + FOUR_SIZE_T_SIZES + CHUNK_ALIGN_MASK); - size_t offset = align_offset(chunk2mem(rawsp)); - char* asp = rawsp + offset; - char* csp = (asp < (old_top + MIN_CHUNK_SIZE))? old_top : asp; - mchunkptr sp = (mchunkptr)csp; - msegmentptr ss = (msegmentptr)(chunk2mem(sp)); - mchunkptr tnext = chunk_plus_offset(sp, ssize); - mchunkptr p = tnext; - int nfences = 0; - - /* reset top to new space */ - init_top(m, (mchunkptr)tbase, tsize - TOP_FOOT_SIZE); - - /* Set up segment record */ - assert(is_aligned(ss)); - set_size_and_pinuse_of_inuse_chunk(m, sp, ssize); - *ss = m->seg; /* Push current record */ - m->seg.base = tbase; - m->seg.size = tsize; - m->seg.sflags = mmapped; - m->seg.next = ss; - - /* Insert trailing fenceposts */ - for (;;) { - mchunkptr nextp = chunk_plus_offset(p, SIZE_T_SIZE); - p->head = FENCEPOST_HEAD; - ++nfences; - if ((char*)(&(nextp->head)) < old_end) - p = nextp; - else - break; - } - assert(nfences >= 2); - - /* Insert the rest of old top into a bin as an ordinary free chunk */ - if (csp != old_top) { - mchunkptr q = (mchunkptr)old_top; - size_t psize = csp - old_top; - mchunkptr tn = chunk_plus_offset(q, psize); - set_free_with_pinuse(q, psize, tn); - insert_chunk(m, q, psize); - } - - check_top_chunk(m, m->top); + /* Determine locations and sizes of segment, fenceposts, old top */ + char* old_top = (char*)m->top; + msegmentptr oldsp = segment_holding(m, old_top); + char* old_end = oldsp->base + oldsp->size; + size_t ssize = pad_request(sizeof(struct malloc_segment)); + char* rawsp = old_end - (ssize + FOUR_SIZE_T_SIZES + CHUNK_ALIGN_MASK); + size_t offset = align_offset(chunk2mem(rawsp)); + char* asp = rawsp + offset; + char* csp = (asp < (old_top + MIN_CHUNK_SIZE))? old_top : asp; + mchunkptr sp = (mchunkptr)csp; + msegmentptr ss = (msegmentptr)(chunk2mem(sp)); + mchunkptr tnext = chunk_plus_offset(sp, ssize); + mchunkptr p = tnext; + int nfences = 0; + + /* reset top to new space */ + init_top(m, (mchunkptr)tbase, tsize - TOP_FOOT_SIZE); + + /* Set up segment record */ + assert(is_aligned(ss)); + set_size_and_pinuse_of_inuse_chunk(m, sp, ssize); + *ss = m->seg; /* Push current record */ + m->seg.base = tbase; + m->seg.size = tsize; + m->seg.sflags = mmapped; + m->seg.next = ss; + + /* Insert trailing fenceposts */ + for (;;) { + mchunkptr nextp = chunk_plus_offset(p, SIZE_T_SIZE); + p->head = FENCEPOST_HEAD; + ++nfences; + if ((char*)(&(nextp->head)) < old_end) + p = nextp; + else + break; + } + assert(nfences >= 2); + + /* Insert the rest of old top into a bin as an ordinary free chunk */ + if (csp != old_top) { + mchunkptr q = (mchunkptr)old_top; + size_t psize = csp - old_top; + mchunkptr tn = chunk_plus_offset(q, psize); + set_free_with_pinuse(q, psize, tn); + insert_chunk(m, q, psize); + } + + check_top_chunk(m, m->top); } /* -------------------------- System allocation -------------------------- */ /* Get memory from system using MORECORE or MMAP */ static void* sys_alloc(mstate m, size_t nb) { - char* tbase = CMFAIL; - size_t tsize = 0; - flag_t mmap_flag = 0; - - ensure_initialization(); - - /* Directly map large chunks, but only if already initialized */ - if (use_mmap(m) && nb >= mparams.mmap_threshold && m->topsize != 0) { - void* mem = mmap_alloc(m, nb); - if (mem != 0) - return mem; - } - - /* - Try getting memory in any of three ways (in most-preferred to - least-preferred order): - 1. A call to MORECORE that can normally contiguously extend memory. - (disabled if not MORECORE_CONTIGUOUS or not HAVE_MORECORE or - or main space is mmapped or a previous contiguous call failed) - 2. A call to MMAP new space (disabled if not HAVE_MMAP). - Note that under the default settings, if MORECORE is unable to - fulfill a request, and HAVE_MMAP is true, then mmap is - used as a noncontiguous system allocator. This is a useful backup - strategy for systems with holes in address spaces -- in this case - sbrk cannot contiguously expand the heap, but mmap may be able to - find space. - 3. A call to MORECORE that cannot usually contiguously extend memory. - (disabled if not HAVE_MORECORE) - - In all cases, we need to request enough bytes from system to ensure - we can malloc nb bytes upon success, so pad with enough space for - top_foot, plus alignment-pad to make sure we don't lose bytes if - not on boundary, and round this up to a granularity unit. - */ - - if (MORECORE_CONTIGUOUS && !use_noncontiguous(m)) { - char* br = CMFAIL; - msegmentptr ss = (m->top == 0)? 0 : segment_holding(m, (char*)m->top); - size_t asize = 0; - ACQUIRE_MALLOC_GLOBAL_LOCK(); - - if (ss == 0) { /* First time through or recovery */ - char* base = (char*)CALL_MORECORE(0); - if (base != CMFAIL) { - asize = granularity_align(nb + SYS_ALLOC_PADDING); - /* Adjust to end on a page boundary */ - if (!is_page_aligned(base)) - asize += (page_align((size_t)base) - (size_t)base); - /* Can't call MORECORE if size is negative when treated as signed */ - if (asize < HALF_MAX_SIZE_T && - (br = (char*)(CALL_MORECORE(asize))) == base) { - tbase = base; - tsize = asize; - } - } + char* tbase = CMFAIL; + size_t tsize = 0; + flag_t mmap_flag = 0; + size_t asize; /* allocation size */ + + ensure_initialization(); + + /* Directly map large chunks, but only if already initialized */ + if (use_mmap(m) && nb >= mparams.mmap_threshold && m->topsize != 0) { + void* mem = mmap_alloc(m, nb); + if (mem != 0) + return mem; } - else { - /* Subtract out existing available top space from MORECORE request. */ - asize = granularity_align(nb - m->topsize + SYS_ALLOC_PADDING); - /* Use mem here only if it did continuously extend old space */ - if (asize < HALF_MAX_SIZE_T && - (br = (char*)(CALL_MORECORE(asize))) == ss->base+ss->size) { - tbase = br; - tsize = asize; - } + + asize = granularity_align(nb + SYS_ALLOC_PADDING); + if (asize <= nb) + return 0; /* wraparound */ + if (m->footprint_limit != 0) { + size_t fp = m->footprint + asize; + if (fp <= m->footprint || fp > m->footprint_limit) + return 0; } - - if (tbase == CMFAIL) { /* Cope with partial failure */ - if (br != CMFAIL) { /* Try to use/extend the space we did get */ - if (asize < HALF_MAX_SIZE_T && - asize < nb + SYS_ALLOC_PADDING) { - size_t esize = granularity_align(nb + SYS_ALLOC_PADDING - asize); - if (esize < HALF_MAX_SIZE_T) { - char* end = (char*)CALL_MORECORE(esize); - if (end != CMFAIL) - asize += esize; - else { /* Can't use; try to release */ - (void) CALL_MORECORE(-asize); - br = CMFAIL; + + /* + Try getting memory in any of three ways (in most-preferred to + least-preferred order): + 1. A call to MORECORE that can normally contiguously extend memory. + (disabled if not MORECORE_CONTIGUOUS or not HAVE_MORECORE or + or main space is mmapped or a previous contiguous call failed) + 2. A call to MMAP new space (disabled if not HAVE_MMAP). + Note that under the default settings, if MORECORE is unable to + fulfill a request, and HAVE_MMAP is true, then mmap is + used as a noncontiguous system allocator. This is a useful backup + strategy for systems with holes in address spaces -- in this case + sbrk cannot contiguously expand the heap, but mmap may be able to + find space. + 3. A call to MORECORE that cannot usually contiguously extend memory. + (disabled if not HAVE_MORECORE) + + In all cases, we need to request enough bytes from system to ensure + we can malloc nb bytes upon success, so pad with enough space for + top_foot, plus alignment-pad to make sure we don't lose bytes if + not on boundary, and round this up to a granularity unit. + */ + + if (MORECORE_CONTIGUOUS && !use_noncontiguous(m)) { + char* br = CMFAIL; + size_t ssize = asize; /* sbrk call size */ + msegmentptr ss = (m->top == 0)? 0 : segment_holding(m, (char*)m->top); + ACQUIRE_MALLOC_GLOBAL_LOCK(); + + if (ss == 0) { /* First time through or recovery */ + char* base = (char*)CALL_MORECORE(0); + if (base != CMFAIL) { + size_t fp; + /* Adjust to end on a page boundary */ + if (!is_page_aligned(base)) + ssize += (page_align((size_t)base) - (size_t)base); + fp = m->footprint + ssize; /* recheck limits */ + if (ssize > nb && ssize < HALF_MAX_SIZE_T && + (m->footprint_limit == 0 || + (fp > m->footprint && fp <= m->footprint_limit)) && + (br = (char*)(CALL_MORECORE(ssize))) == base) { + tbase = base; + tsize = ssize; + } + } + } + else { + /* Subtract out existing available top space from MORECORE request. */ + ssize = granularity_align(nb - m->topsize + SYS_ALLOC_PADDING); + /* Use mem here only if it did continuously extend old space */ + if (ssize < HALF_MAX_SIZE_T && + (br = (char*)(CALL_MORECORE(ssize))) == ss->base+ss->size) { + tbase = br; + tsize = ssize; } - } } - } - if (br != CMFAIL) { /* Use the space we did get */ - tbase = br; - tsize = asize; - } - else - disable_contiguous(m); /* Don't try contiguous path in the future */ + + if (tbase == CMFAIL) { /* Cope with partial failure */ + if (br != CMFAIL) { /* Try to use/extend the space we did get */ + if (ssize < HALF_MAX_SIZE_T && + ssize < nb + SYS_ALLOC_PADDING) { + size_t esize = granularity_align(nb + SYS_ALLOC_PADDING - ssize); + if (esize < HALF_MAX_SIZE_T) { + char* end = (char*)CALL_MORECORE(esize); + if (end != CMFAIL) + ssize += esize; + else { /* Can't use; try to release */ + (void) CALL_MORECORE(-ssize); + br = CMFAIL; + } + } + } + } + if (br != CMFAIL) { /* Use the space we did get */ + tbase = br; + tsize = ssize; + } + else + disable_contiguous(m); /* Don't try contiguous path in the future */ + } + + RELEASE_MALLOC_GLOBAL_LOCK(); } - - RELEASE_MALLOC_GLOBAL_LOCK(); - } - - if (HAVE_MMAP && tbase == CMFAIL) { /* Try MMAP */ - size_t rsize = granularity_align(nb + SYS_ALLOC_PADDING); - if (rsize > nb) { /* Fail if wraps around zero */ - char* mp = (char*)(CALL_MMAP(rsize)); - if (mp != CMFAIL) { - tbase = mp; - tsize = rsize; - mmap_flag = USE_MMAP_BIT; - } + + if (HAVE_MMAP && tbase == CMFAIL) { /* Try MMAP */ + char* mp = (char*)(CALL_MMAP(asize)); + if (mp != CMFAIL) { + tbase = mp; + tsize = asize; + mmap_flag = USE_MMAP_BIT; + } } - } - - if (HAVE_MORECORE && tbase == CMFAIL) { /* Try noncontiguous MORECORE */ - size_t asize = granularity_align(nb + SYS_ALLOC_PADDING); - if (asize < HALF_MAX_SIZE_T) { - char* br = CMFAIL; - char* end = CMFAIL; - ACQUIRE_MALLOC_GLOBAL_LOCK(); - br = (char*)(CALL_MORECORE(asize)); - end = (char*)(CALL_MORECORE(0)); - RELEASE_MALLOC_GLOBAL_LOCK(); - if (br != CMFAIL && end != CMFAIL && br < end) { - size_t ssize = end - br; - if (ssize > nb + TOP_FOOT_SIZE) { - tbase = br; - tsize = ssize; + + if (HAVE_MORECORE && tbase == CMFAIL) { /* Try noncontiguous MORECORE */ + if (asize < HALF_MAX_SIZE_T) { + char* br = CMFAIL; + char* end = CMFAIL; + ACQUIRE_MALLOC_GLOBAL_LOCK(); + br = (char*)(CALL_MORECORE(asize)); + end = (char*)(CALL_MORECORE(0)); + RELEASE_MALLOC_GLOBAL_LOCK(); + if (br != CMFAIL && end != CMFAIL && br < end) { + size_t ssize = end - br; + if (ssize > nb + TOP_FOOT_SIZE) { + tbase = br; + tsize = ssize; + } + } } - } } - } - - if (tbase != CMFAIL) { - - if ((m->footprint += tsize) > m->max_footprint) - m->max_footprint = m->footprint; - - if (!is_initialized(m)) { /* first-time initialization */ - if (m->least_addr == 0 || tbase < m->least_addr) - m->least_addr = tbase; - m->seg.base = tbase; - m->seg.size = tsize; - m->seg.sflags = mmap_flag; - m->magic = mparams.magic; - m->release_checks = MAX_RELEASE_CHECK_RATE; - init_bins(m); + + if (tbase != CMFAIL) { + + if ((m->footprint += tsize) > m->max_footprint) + m->max_footprint = m->footprint; + + if (!is_initialized(m)) { /* first-time initialization */ + if (m->least_addr == 0 || tbase < m->least_addr) + m->least_addr = tbase; + m->seg.base = tbase; + m->seg.size = tsize; + m->seg.sflags = mmap_flag; + m->magic = mparams.magic; + m->release_checks = MAX_RELEASE_CHECK_RATE; + init_bins(m); #if !ONLY_MSPACES - if (is_global(m)) - init_top(m, (mchunkptr)tbase, tsize - TOP_FOOT_SIZE); - else + if (is_global(m)) + init_top(m, (mchunkptr)tbase, tsize - TOP_FOOT_SIZE); + else #endif - { - /* Offset top by embedded malloc_state */ - mchunkptr mn = next_chunk(mem2chunk(m)); - init_top(m, mn, (size_t)((tbase + tsize) - (char*)mn) -TOP_FOOT_SIZE); - } - } - - else { - /* Try to merge with an existing segment */ - msegmentptr sp = &m->seg; - /* Only consider most recent segment if traversal suppressed */ - while (sp != 0 && tbase != sp->base + sp->size) - sp = (NO_SEGMENT_TRAVERSAL) ? 0 : sp->next; - if (sp != 0 && - !is_extern_segment(sp) && - (sp->sflags & USE_MMAP_BIT) == mmap_flag && - segment_holds(sp, m->top)) { /* append */ - sp->size += tsize; - init_top(m, m->top, m->topsize + tsize); - } - else { - if (tbase < m->least_addr) - m->least_addr = tbase; - sp = &m->seg; - while (sp != 0 && sp->base != tbase + tsize) - sp = (NO_SEGMENT_TRAVERSAL) ? 0 : sp->next; - if (sp != 0 && - !is_extern_segment(sp) && - (sp->sflags & USE_MMAP_BIT) == mmap_flag) { - char* oldbase = sp->base; - sp->base = tbase; - sp->size += tsize; - return prepend_alloc(m, tbase, oldbase, nb); + { + /* Offset top by embedded malloc_state */ + mchunkptr mn = next_chunk(mem2chunk(m)); + init_top(m, mn, (size_t)((tbase + tsize) - (char*)mn) -TOP_FOOT_SIZE); + } + } + + else { + /* Try to merge with an existing segment */ + msegmentptr sp = &m->seg; + /* Only consider most recent segment if traversal suppressed */ + while (sp != 0 && tbase != sp->base + sp->size) + sp = (NO_SEGMENT_TRAVERSAL) ? 0 : sp->next; + if (sp != 0 && + !is_extern_segment(sp) && + (sp->sflags & USE_MMAP_BIT) == mmap_flag && + segment_holds(sp, m->top)) { /* append */ + sp->size += tsize; + init_top(m, m->top, m->topsize + tsize); + } + else { + if (tbase < m->least_addr) + m->least_addr = tbase; + sp = &m->seg; + while (sp != 0 && sp->base != tbase + tsize) + sp = (NO_SEGMENT_TRAVERSAL) ? 0 : sp->next; + if (sp != 0 && + !is_extern_segment(sp) && + (sp->sflags & USE_MMAP_BIT) == mmap_flag) { + char* oldbase = sp->base; + sp->base = tbase; + sp->size += tsize; + return prepend_alloc(m, tbase, oldbase, nb); + } + else + add_segment(m, tbase, tsize, mmap_flag); + } + } + + if (nb < m->topsize) { /* Allocate from new or extended top space */ + size_t rsize = m->topsize -= nb; + mchunkptr p = m->top; + mchunkptr r = m->top = chunk_plus_offset(p, nb); + r->head = rsize | PINUSE_BIT; + set_size_and_pinuse_of_inuse_chunk(m, p, nb); + check_top_chunk(m, m->top); + check_malloced_chunk(m, chunk2mem(p), nb); + return chunk2mem(p); } - else - add_segment(m, tbase, tsize, mmap_flag); - } - } - - if (nb < m->topsize) { /* Allocate from new or extended top space */ - size_t rsize = m->topsize -= nb; - mchunkptr p = m->top; - mchunkptr r = m->top = chunk_plus_offset(p, nb); - r->head = rsize | PINUSE_BIT; - set_size_and_pinuse_of_inuse_chunk(m, p, nb); - check_top_chunk(m, m->top); - check_malloced_chunk(m, chunk2mem(p), nb); - return chunk2mem(p); } - } - - MALLOC_FAILURE_ACTION; - return 0; + + MALLOC_FAILURE_ACTION; + return 0; } /* ----------------------- system deallocation -------------------------- */ /* Unmap and unlink any mmapped segments that don't contain used chunks */ static size_t release_unused_segments(mstate m) { - size_t released = 0; - int nsegs = 0; - msegmentptr pred = &m->seg; - msegmentptr sp = pred->next; - while (sp != 0) { - char* base = sp->base; - size_t size = sp->size; - msegmentptr next = sp->next; - ++nsegs; - if (is_mmapped_segment(sp) && !is_extern_segment(sp)) { - mchunkptr p = align_as_chunk(base); - size_t psize = chunksize(p); - /* Can unmap if first chunk holds entire segment and not pinned */ - if (!is_inuse(p) && (char*)p + psize >= base + size - TOP_FOOT_SIZE) { - tchunkptr tp = (tchunkptr)p; - assert(segment_holds(sp, (char*)sp)); - if (p == m->dv) { - m->dv = 0; - m->dvsize = 0; - } - else { - unlink_large_chunk(m, tp); - } - if (CALL_MUNMAP(base, size) == 0) { - released += size; - m->footprint -= size; - /* unlink obsoleted record */ - sp = pred; - sp->next = next; - } - else { /* back out if cannot unmap */ - insert_large_chunk(m, tp, psize); + size_t released = 0; + int nsegs = 0; + msegmentptr pred = &m->seg; + msegmentptr sp = pred->next; + while (sp != 0) { + char* base = sp->base; + size_t size = sp->size; + msegmentptr next = sp->next; + ++nsegs; + if (is_mmapped_segment(sp) && !is_extern_segment(sp)) { + mchunkptr p = align_as_chunk(base); + size_t psize = chunksize(p); + /* Can unmap if first chunk holds entire segment and not pinned */ + if (!is_inuse(p) && (char*)p + psize >= base + size - TOP_FOOT_SIZE) { + tchunkptr tp = (tchunkptr)p; + assert(segment_holds(sp, (char*)sp)); + if (p == m->dv) { + m->dv = 0; + m->dvsize = 0; + } + else { + unlink_large_chunk(m, tp); + } + if (CALL_MUNMAP(base, size) == 0) { + released += size; + m->footprint -= size; + /* unlink obsoleted record */ + sp = pred; + sp->next = next; + } + else { /* back out if cannot unmap */ + insert_large_chunk(m, tp, psize); + } + } } - } + if (NO_SEGMENT_TRAVERSAL) /* scan only first segment */ + break; + pred = sp; + sp = next; } - if (NO_SEGMENT_TRAVERSAL) /* scan only first segment */ - break; - pred = sp; - sp = next; - } - /* Reset check counter */ - m->release_checks = ((nsegs > MAX_RELEASE_CHECK_RATE)? - nsegs : MAX_RELEASE_CHECK_RATE); - return released; + /* Reset check counter */ + m->release_checks = (((size_t) nsegs > (size_t) MAX_RELEASE_CHECK_RATE)? + (size_t) nsegs : (size_t) MAX_RELEASE_CHECK_RATE); + return released; } static int sys_trim(mstate m, size_t pad) { - size_t released = 0; - ensure_initialization(); - if (pad < MAX_REQUEST && is_initialized(m)) { - pad += TOP_FOOT_SIZE; /* ensure enough room for segment overhead */ - - if (m->topsize > pad) { - /* Shrink top space in granularity-size units, keeping at least one */ - size_t unit = mparams.granularity; - size_t extra = ((m->topsize - pad + (unit - SIZE_T_ONE)) / unit - - SIZE_T_ONE) * unit; - msegmentptr sp = segment_holding(m, (char*)m->top); - - if (!is_extern_segment(sp)) { - if (is_mmapped_segment(sp)) { - if (HAVE_MMAP && - sp->size >= extra && - !has_segment_link(m, sp)) { /* can't shrink if pinned */ - size_t newsize = sp->size - extra; - (void)newsize; // XXX EMSCRIPTEN - /* Prefer mremap, fall back to munmap */ - if ((CALL_MREMAP(sp->base, sp->size, newsize, 0) != MFAIL) || - (CALL_MUNMAP(sp->base + newsize, extra) == 0)) { - released = extra; + size_t released = 0; + ensure_initialization(); + if (pad < MAX_REQUEST && is_initialized(m)) { + pad += TOP_FOOT_SIZE; /* ensure enough room for segment overhead */ + + if (m->topsize > pad) { + /* Shrink top space in granularity-size units, keeping at least one */ + size_t unit = mparams.granularity; + size_t extra = ((m->topsize - pad + (unit - SIZE_T_ONE)) / unit - + SIZE_T_ONE) * unit; + msegmentptr sp = segment_holding(m, (char*)m->top); + + if (!is_extern_segment(sp)) { + if (is_mmapped_segment(sp)) { + if (HAVE_MMAP && + sp->size >= extra && + !has_segment_link(m, sp)) { /* can't shrink if pinned */ + size_t newsize = sp->size - extra; + (void)newsize; /* placate people compiling -Wunused-variable */ + /* Prefer mremap, fall back to munmap */ + if ((CALL_MREMAP(sp->base, sp->size, newsize, 0) != MFAIL) || + (CALL_MUNMAP(sp->base + newsize, extra) == 0)) { + released = extra; + } + } + } + else if (HAVE_MORECORE) { + if (extra >= HALF_MAX_SIZE_T) /* Avoid wrapping negative */ + extra = (HALF_MAX_SIZE_T) + SIZE_T_ONE - unit; + ACQUIRE_MALLOC_GLOBAL_LOCK(); + { + /* Make sure end of memory is where we last set it. */ + char* old_br = (char*)(CALL_MORECORE(0)); + if (old_br == sp->base + sp->size) { + char* rel_br = (char*)(CALL_MORECORE(-extra)); + char* new_br = (char*)(CALL_MORECORE(0)); + if (rel_br != CMFAIL && new_br < old_br) + released = old_br - new_br; + } + } + RELEASE_MALLOC_GLOBAL_LOCK(); + } } - } - } - else if (HAVE_MORECORE) { - if (extra >= HALF_MAX_SIZE_T) /* Avoid wrapping negative */ - extra = (HALF_MAX_SIZE_T) + SIZE_T_ONE - unit; - ACQUIRE_MALLOC_GLOBAL_LOCK(); - { - /* Make sure end of memory is where we last set it. */ - char* old_br = (char*)(CALL_MORECORE(0)); - if (old_br == sp->base + sp->size) { - char* rel_br = (char*)(CALL_MORECORE(-extra)); - char* new_br = (char*)(CALL_MORECORE(0)); - if (rel_br != CMFAIL && new_br < old_br) - released = old_br - new_br; + + if (released != 0) { + sp->size -= released; + m->footprint -= released; + init_top(m, m->top, m->topsize - released); + check_top_chunk(m, m->top); } - } - RELEASE_MALLOC_GLOBAL_LOCK(); } - } - - if (released != 0) { - sp->size -= released; - m->footprint -= released; - init_top(m, m->top, m->topsize - released); - check_top_chunk(m, m->top); - } + + /* Unmap any unused mmapped segments */ + if (HAVE_MMAP) + released += release_unused_segments(m); + + /* On failure, disable autotrim to avoid repeated failed future calls */ + if (released == 0 && m->topsize > m->trim_check) + m->trim_check = MAX_SIZE_T; } - - /* Unmap any unused mmapped segments */ - if (HAVE_MMAP) - released += release_unused_segments(m); - - /* On failure, disable autotrim to avoid repeated failed future calls */ - if (released == 0 && m->topsize > m->trim_check) - m->trim_check = MAX_SIZE_T; - } - - return (released != 0)? 1 : 0; + + return (released != 0)? 1 : 0; } +/* Consolidate and bin a chunk. Differs from exported versions + of free mainly in that the chunk need not be marked as inuse. + */ +static void dispose_chunk(mstate m, mchunkptr p, size_t psize) { + mchunkptr next = chunk_plus_offset(p, psize); + if (!pinuse(p)) { + mchunkptr prev; + size_t prevsize = p->prev_foot; + if (is_mmapped(p)) { + psize += prevsize + MMAP_FOOT_PAD; + if (CALL_MUNMAP((char*)p - prevsize, psize) == 0) + m->footprint -= psize; + return; + } + prev = chunk_minus_offset(p, prevsize); + psize += prevsize; + p = prev; + if (RTCHECK(ok_address(m, prev))) { /* consolidate backward */ + if (p != m->dv) { + unlink_chunk(m, p, prevsize); + } + else if ((next->head & INUSE_BITS) == INUSE_BITS) { + m->dvsize = psize; + set_free_with_pinuse(p, psize, next); + return; + } + } + else { + CORRUPTION_ERROR_ACTION(m); + return; + } + } + if (RTCHECK(ok_address(m, next))) { + if (!cinuse(next)) { /* consolidate forward */ + if (next == m->top) { + size_t tsize = m->topsize += psize; + m->top = p; + p->head = tsize | PINUSE_BIT; + if (p == m->dv) { + m->dv = 0; + m->dvsize = 0; + } + return; + } + else if (next == m->dv) { + size_t dsize = m->dvsize += psize; + m->dv = p; + set_size_and_pinuse_of_free_chunk(p, dsize); + return; + } + else { + size_t nsize = chunksize(next); + psize += nsize; + unlink_chunk(m, next, nsize); + set_size_and_pinuse_of_free_chunk(p, psize); + if (p == m->dv) { + m->dvsize = psize; + return; + } + } + } + else { + set_free_with_pinuse(p, psize, next); + } + insert_chunk(m, p, psize); + } + else { + CORRUPTION_ERROR_ACTION(m); + } +} -/* ---------------------------- malloc support --------------------------- */ +/* ---------------------------- malloc --------------------------- */ /* allocate a large request from the best fitting chunk in a treebin */ static void* tmalloc_large(mstate m, size_t nb) { - tchunkptr v = 0; - size_t rsize = -nb; /* Unsigned negation */ - tchunkptr t; - bindex_t idx; - compute_tree_index(nb, idx); - if ((t = *treebin_at(m, idx)) != 0) { - /* Traverse tree for this bin looking for node with size == nb */ - size_t sizebits = nb << leftshift_for_tree_index(idx); - tchunkptr rst = 0; /* The deepest untaken right subtree */ - for (;;) { - tchunkptr rt; - size_t trem = chunksize(t) - nb; - if (trem < rsize) { - v = t; - if ((rsize = trem) == 0) - break; - } - rt = t->child[1]; - t = t->child[(sizebits >> (SIZE_T_BITSIZE-SIZE_T_ONE)) & 1]; - if (rt != 0 && rt != t) - rst = rt; - if (t == 0) { - t = rst; /* set t to least subtree holding sizes > nb */ - break; - } - sizebits <<= 1; + tchunkptr v = 0; + size_t rsize = -nb; /* Unsigned negation */ + tchunkptr t; + bindex_t idx; + compute_tree_index(nb, idx); + if ((t = *treebin_at(m, idx)) != 0) { + /* Traverse tree for this bin looking for node with size == nb */ + size_t sizebits = nb << leftshift_for_tree_index(idx); + tchunkptr rst = 0; /* The deepest untaken right subtree */ + for (;;) { + tchunkptr rt; + size_t trem = chunksize(t) - nb; + if (trem < rsize) { + v = t; + if ((rsize = trem) == 0) + break; + } + rt = t->child[1]; + t = t->child[(sizebits >> (SIZE_T_BITSIZE-SIZE_T_ONE)) & 1]; + if (rt != 0 && rt != t) + rst = rt; + if (t == 0) { + t = rst; /* set t to least subtree holding sizes > nb */ + break; + } + sizebits <<= 1; + } } - } - if (t == 0 && v == 0) { /* set t to root of next non-empty treebin */ - binmap_t leftbits = left_bits(idx2bit(idx)) & m->treemap; - if (leftbits != 0) { - bindex_t i; - binmap_t leastbit = least_bit(leftbits); - compute_bit2idx(leastbit, i); - t = *treebin_at(m, i); + if (t == 0 && v == 0) { /* set t to root of next non-empty treebin */ + binmap_t leftbits = left_bits(idx2bit(idx)) & m->treemap; + if (leftbits != 0) { + bindex_t i; + binmap_t leastbit = least_bit(leftbits); + compute_bit2idx(leastbit, i); + t = *treebin_at(m, i); + } } - } - - while (t != 0) { /* find smallest of tree or subtree */ - size_t trem = chunksize(t) - nb; - if (trem < rsize) { - rsize = trem; - v = t; + + while (t != 0) { /* find smallest of tree or subtree */ + size_t trem = chunksize(t) - nb; + if (trem < rsize) { + rsize = trem; + v = t; + } + t = leftmost_child(t); } - t = leftmost_child(t); - } - - /* If dv is a better fit, return 0 so malloc will use it */ - if (v != 0 && rsize < (size_t)(m->dvsize - nb)) { - if (RTCHECK(ok_address(m, v))) { /* split */ - mchunkptr r = chunk_plus_offset(v, nb); - assert(chunksize(v) == rsize + nb); - if (RTCHECK(ok_next(v, r))) { - unlink_large_chunk(m, v); - if (rsize < MIN_CHUNK_SIZE) - set_inuse_and_pinuse(m, v, (rsize + nb)); - else { - set_size_and_pinuse_of_inuse_chunk(m, v, nb); - set_size_and_pinuse_of_free_chunk(r, rsize); - insert_chunk(m, r, rsize); + + /* If dv is a better fit, return 0 so malloc will use it */ + if (v != 0 && rsize < (size_t)(m->dvsize - nb)) { + if (RTCHECK(ok_address(m, v))) { /* split */ + mchunkptr r = chunk_plus_offset(v, nb); + assert(chunksize(v) == rsize + nb); + if (RTCHECK(ok_next(v, r))) { + unlink_large_chunk(m, v); + if (rsize < MIN_CHUNK_SIZE) + set_inuse_and_pinuse(m, v, (rsize + nb)); + else { + set_size_and_pinuse_of_inuse_chunk(m, v, nb); + set_size_and_pinuse_of_free_chunk(r, rsize); + insert_chunk(m, r, rsize); + } + return chunk2mem(v); + } } - return chunk2mem(v); - } + CORRUPTION_ERROR_ACTION(m); } - CORRUPTION_ERROR_ACTION(m); - } - return 0; + return 0; } /* allocate a small request from the best fitting chunk in a treebin */ static void* tmalloc_small(mstate m, size_t nb) { - tchunkptr t, v; - size_t rsize; - bindex_t i; - binmap_t leastbit = least_bit(m->treemap); - compute_bit2idx(leastbit, i); - v = t = *treebin_at(m, i); - rsize = chunksize(t) - nb; - - while ((t = leftmost_child(t)) != 0) { - size_t trem = chunksize(t) - nb; - if (trem < rsize) { - rsize = trem; - v = t; + tchunkptr t, v; + size_t rsize; + bindex_t i; + binmap_t leastbit = least_bit(m->treemap); + compute_bit2idx(leastbit, i); + v = t = *treebin_at(m, i); + rsize = chunksize(t) - nb; + + while ((t = leftmost_child(t)) != 0) { + size_t trem = chunksize(t) - nb; + if (trem < rsize) { + rsize = trem; + v = t; + } } - } - - if (RTCHECK(ok_address(m, v))) { - mchunkptr r = chunk_plus_offset(v, nb); - assert(chunksize(v) == rsize + nb); - if (RTCHECK(ok_next(v, r))) { - unlink_large_chunk(m, v); - if (rsize < MIN_CHUNK_SIZE) - set_inuse_and_pinuse(m, v, (rsize + nb)); - else { - set_size_and_pinuse_of_inuse_chunk(m, v, nb); - set_size_and_pinuse_of_free_chunk(r, rsize); - replace_dv(m, r, rsize); - } - return chunk2mem(v); + + if (RTCHECK(ok_address(m, v))) { + mchunkptr r = chunk_plus_offset(v, nb); + assert(chunksize(v) == rsize + nb); + if (RTCHECK(ok_next(v, r))) { + unlink_large_chunk(m, v); + if (rsize < MIN_CHUNK_SIZE) + set_inuse_and_pinuse(m, v, (rsize + nb)); + else { + set_size_and_pinuse_of_inuse_chunk(m, v, nb); + set_size_and_pinuse_of_free_chunk(r, rsize); + replace_dv(m, r, rsize); + } + return chunk2mem(v); + } } - } - - CORRUPTION_ERROR_ACTION(m); - return 0; + + CORRUPTION_ERROR_ACTION(m); + return 0; } -/* --------------------------- realloc support --------------------------- */ +#if !ONLY_MSPACES -static void* internal_realloc(mstate m, void* oldmem, size_t bytes) { - if (bytes >= MAX_REQUEST) { - MALLOC_FAILURE_ACTION; - return 0; - } - if (!PREACTION(m)) { - mchunkptr oldp = mem2chunk(oldmem); - size_t oldsize = chunksize(oldp); - mchunkptr next = chunk_plus_offset(oldp, oldsize); - mchunkptr newp = 0; - void* extra = 0; - - /* Try to either shrink or extend into top. Else malloc-copy-free */ - - if (RTCHECK(ok_address(m, oldp) && ok_inuse(oldp) && - ok_next(oldp, next) && ok_pinuse(next))) { - size_t nb = request2size(bytes); - if (is_mmapped(oldp)) - newp = mmap_resize(m, oldp, nb); - else if (oldsize >= nb) { /* already big enough */ - size_t rsize = oldsize - nb; - newp = oldp; - if (rsize >= MIN_CHUNK_SIZE) { - mchunkptr remainder = chunk_plus_offset(newp, nb); - set_inuse(m, newp, nb); - set_inuse_and_pinuse(m, remainder, rsize); - extra = chunk2mem(remainder); +void* dlmalloc(size_t bytes) { + /* + Basic algorithm: + If a small request (< 256 bytes minus per-chunk overhead): + 1. If one exists, use a remainderless chunk in associated smallbin. + (Remainderless means that there are too few excess bytes to + represent as a chunk.) + 2. If it is big enough, use the dv chunk, which is normally the + chunk adjacent to the one used for the most recent small request. + 3. If one exists, split the smallest available chunk in a bin, + saving remainder in dv. + 4. If it is big enough, use the top chunk. + 5. If available, get memory from system and use it + Otherwise, for a large request: + 1. Find the smallest available binned chunk that fits, and use it + if it is better fitting than dv chunk, splitting if necessary. + 2. If better fitting than any binned chunk, use the dv chunk. + 3. If it is big enough, use the top chunk. + 4. If request size >= mmap threshold, try to directly mmap this chunk. + 5. If available, get memory from system and use it + + The ugly goto's here ensure that postaction occurs along all paths. + */ + +#if USE_LOCKS + ensure_initialization(); /* initialize in sys_alloc if not using locks */ +#endif + + if (!PREACTION(gm)) { + void* mem; + size_t nb; + if (bytes <= MAX_SMALL_REQUEST) { + bindex_t idx; + binmap_t smallbits; + nb = (bytes < MIN_REQUEST)? MIN_CHUNK_SIZE : pad_request(bytes); + idx = small_index(nb); + smallbits = gm->smallmap >> idx; + + if ((smallbits & 0x3U) != 0) { /* Remainderless fit to a smallbin. */ + mchunkptr b, p; + idx += ~smallbits & 1; /* Uses next bin if idx empty */ + b = smallbin_at(gm, idx); + p = b->fd; + assert(chunksize(p) == small_index2size(idx)); + unlink_first_small_chunk(gm, b, p, idx); + set_inuse_and_pinuse(gm, p, small_index2size(idx)); + mem = chunk2mem(p); + check_malloced_chunk(gm, mem, nb); + goto postaction; + } + + else if (nb > gm->dvsize) { + if (smallbits != 0) { /* Use chunk in next nonempty smallbin */ + mchunkptr b, p, r; + size_t rsize; + bindex_t i; + binmap_t leftbits = (smallbits << idx) & left_bits(idx2bit(idx)); + binmap_t leastbit = least_bit(leftbits); + compute_bit2idx(leastbit, i); + b = smallbin_at(gm, i); + p = b->fd; + assert(chunksize(p) == small_index2size(i)); + unlink_first_small_chunk(gm, b, p, i); + rsize = small_index2size(i) - nb; + /* Fit here cannot be remainderless if 4byte sizes */ + if (SIZE_T_SIZE != 4 && rsize < MIN_CHUNK_SIZE) + set_inuse_and_pinuse(gm, p, small_index2size(i)); + else { + set_size_and_pinuse_of_inuse_chunk(gm, p, nb); + r = chunk_plus_offset(p, nb); + set_size_and_pinuse_of_free_chunk(r, rsize); + replace_dv(gm, r, rsize); + } + mem = chunk2mem(p); + check_malloced_chunk(gm, mem, nb); + goto postaction; + } + + else if (gm->treemap != 0 && (mem = tmalloc_small(gm, nb)) != 0) { + check_malloced_chunk(gm, mem, nb); + goto postaction; + } + } } - } - else if (next == m->top && oldsize + m->topsize > nb) { - /* Expand into top */ - size_t newsize = oldsize + m->topsize; - size_t newtopsize = newsize - nb; - mchunkptr newtop = chunk_plus_offset(oldp, nb); - set_inuse(m, oldp, nb); - newtop->head = newtopsize |PINUSE_BIT; - m->top = newtop; - m->topsize = newtopsize; - newp = oldp; - } + else if (bytes >= MAX_REQUEST) + nb = MAX_SIZE_T; /* Too big to allocate. Force failure (in sys alloc) */ + else { + nb = pad_request(bytes); + if (gm->treemap != 0 && (mem = tmalloc_large(gm, nb)) != 0) { + check_malloced_chunk(gm, mem, nb); + goto postaction; + } + } + + if (nb <= gm->dvsize) { + size_t rsize = gm->dvsize - nb; + mchunkptr p = gm->dv; + if (rsize >= MIN_CHUNK_SIZE) { /* split dv */ + mchunkptr r = gm->dv = chunk_plus_offset(p, nb); + gm->dvsize = rsize; + set_size_and_pinuse_of_free_chunk(r, rsize); + set_size_and_pinuse_of_inuse_chunk(gm, p, nb); + } + else { /* exhaust dv */ + size_t dvs = gm->dvsize; + gm->dvsize = 0; + gm->dv = 0; + set_inuse_and_pinuse(gm, p, dvs); + } + mem = chunk2mem(p); + check_malloced_chunk(gm, mem, nb); + goto postaction; + } + + else if (nb < gm->topsize) { /* Split top */ + size_t rsize = gm->topsize -= nb; + mchunkptr p = gm->top; + mchunkptr r = gm->top = chunk_plus_offset(p, nb); + r->head = rsize | PINUSE_BIT; + set_size_and_pinuse_of_inuse_chunk(gm, p, nb); + mem = chunk2mem(p); + check_top_chunk(gm, gm->top); + check_malloced_chunk(gm, mem, nb); + goto postaction; + } + + mem = sys_alloc(gm, nb); + + postaction: + POSTACTION(gm); + return mem; } - else { - USAGE_ERROR_ACTION(m, oldmem); - POSTACTION(m); - return 0; + + return 0; +} + +/* ---------------------------- free --------------------------- */ + +void dlfree(void* mem) { + /* + Consolidate freed chunks with preceeding or succeeding bordering + free chunks, if they exist, and then place in a bin. Intermixed + with special cases for top, dv, mmapped chunks, and usage errors. + */ + + if (mem != 0) { + mchunkptr p = mem2chunk(mem); +#if FOOTERS + mstate fm = get_mstate_for(p); + if (!ok_magic(fm)) { + USAGE_ERROR_ACTION(fm, p); + return; + } +#else /* FOOTERS */ +#define fm gm +#endif /* FOOTERS */ + if (!PREACTION(fm)) { + check_inuse_chunk(fm, p); + if (RTCHECK(ok_address(fm, p) && ok_inuse(p))) { + size_t psize = chunksize(p); + mchunkptr next = chunk_plus_offset(p, psize); + if (!pinuse(p)) { + size_t prevsize = p->prev_foot; + if (is_mmapped(p)) { + psize += prevsize + MMAP_FOOT_PAD; + if (CALL_MUNMAP((char*)p - prevsize, psize) == 0) + fm->footprint -= psize; + goto postaction; + } + else { + mchunkptr prev = chunk_minus_offset(p, prevsize); + psize += prevsize; + p = prev; + if (RTCHECK(ok_address(fm, prev))) { /* consolidate backward */ + if (p != fm->dv) { + unlink_chunk(fm, p, prevsize); + } + else if ((next->head & INUSE_BITS) == INUSE_BITS) { + fm->dvsize = psize; + set_free_with_pinuse(p, psize, next); + goto postaction; + } + } + else + goto erroraction; + } + } + + if (RTCHECK(ok_next(p, next) && ok_pinuse(next))) { + if (!cinuse(next)) { /* consolidate forward */ + if (next == fm->top) { + size_t tsize = fm->topsize += psize; + fm->top = p; + p->head = tsize | PINUSE_BIT; + if (p == fm->dv) { + fm->dv = 0; + fm->dvsize = 0; + } + if (should_trim(fm, tsize)) + sys_trim(fm, 0); + goto postaction; + } + else if (next == fm->dv) { + size_t dsize = fm->dvsize += psize; + fm->dv = p; + set_size_and_pinuse_of_free_chunk(p, dsize); + goto postaction; + } + else { + size_t nsize = chunksize(next); + psize += nsize; + unlink_chunk(fm, next, nsize); + set_size_and_pinuse_of_free_chunk(p, psize); + if (p == fm->dv) { + fm->dvsize = psize; + goto postaction; + } + } + } + else + set_free_with_pinuse(p, psize, next); + + if (is_small(psize)) { + insert_small_chunk(fm, p, psize); + check_free_chunk(fm, p); + } + else { + tchunkptr tp = (tchunkptr)p; + insert_large_chunk(fm, tp, psize); + check_free_chunk(fm, p); + if (--fm->release_checks == 0) + release_unused_segments(fm); + } + goto postaction; + } + } + erroraction: + USAGE_ERROR_ACTION(fm, p); + postaction: + POSTACTION(fm); + } } -#if DEBUG - if (newp != 0) { - check_inuse_chunk(m, newp); /* Check requires lock */ +#if !FOOTERS +#undef fm +#endif /* FOOTERS */ +} + +void* dlcalloc(size_t n_elements, size_t elem_size) { + void* mem; + size_t req = 0; + if (n_elements != 0) { + req = n_elements * elem_size; + if (((n_elements | elem_size) & ~(size_t)0xffff) && + (req / n_elements != elem_size)) + req = MAX_SIZE_T; /* force downstream failure on overflow */ } -#endif + mem = dlmalloc(req); + if (mem != 0 && calloc_must_clear(mem2chunk(mem))) + memset(mem, 0, req); + return mem; +} - POSTACTION(m); +#endif /* !ONLY_MSPACES */ + +/* ------------ Internal support for realloc, memalign, etc -------------- */ - if (newp != 0) { - if (extra != 0) { - internal_free(m, extra); - } - return chunk2mem(newp); +/* Try to realloc; only in-place unless can_move true */ +static mchunkptr try_realloc_chunk(mstate m, mchunkptr p, size_t nb, + int can_move) { + mchunkptr newp = 0; + size_t oldsize = chunksize(p); + mchunkptr next = chunk_plus_offset(p, oldsize); + if (RTCHECK(ok_address(m, p) && ok_inuse(p) && + ok_next(p, next) && ok_pinuse(next))) { + if (is_mmapped(p)) { + newp = mmap_resize(m, p, nb, can_move); + } + else if (oldsize >= nb) { /* already big enough */ + size_t rsize = oldsize - nb; + if (rsize >= MIN_CHUNK_SIZE) { /* split off remainder */ + mchunkptr r = chunk_plus_offset(p, nb); + set_inuse(m, p, nb); + set_inuse(m, r, rsize); + dispose_chunk(m, r, rsize); + } + newp = p; + } + else if (next == m->top) { /* extend into top */ + if (oldsize + m->topsize > nb) { + size_t newsize = oldsize + m->topsize; + size_t newtopsize = newsize - nb; + mchunkptr newtop = chunk_plus_offset(p, nb); + set_inuse(m, p, nb); + newtop->head = newtopsize |PINUSE_BIT; + m->top = newtop; + m->topsize = newtopsize; + newp = p; + } + } + else if (next == m->dv) { /* extend into dv */ + size_t dvs = m->dvsize; + if (oldsize + dvs >= nb) { + size_t dsize = oldsize + dvs - nb; + if (dsize >= MIN_CHUNK_SIZE) { + mchunkptr r = chunk_plus_offset(p, nb); + mchunkptr n = chunk_plus_offset(r, dsize); + set_inuse(m, p, nb); + set_size_and_pinuse_of_free_chunk(r, dsize); + clear_pinuse(n); + m->dvsize = dsize; + m->dv = r; + } + else { /* exhaust dv */ + size_t newsize = oldsize + dvs; + set_inuse(m, p, newsize); + m->dvsize = 0; + m->dv = 0; + } + newp = p; + } + } + else if (!cinuse(next)) { /* extend into next free chunk */ + size_t nextsize = chunksize(next); + if (oldsize + nextsize >= nb) { + size_t rsize = oldsize + nextsize - nb; + unlink_chunk(m, next, nextsize); + if (rsize < MIN_CHUNK_SIZE) { + size_t newsize = oldsize + nextsize; + set_inuse(m, p, newsize); + } + else { + mchunkptr r = chunk_plus_offset(p, nb); + set_inuse(m, p, nb); + set_inuse(m, r, rsize); + dispose_chunk(m, r, rsize); + } + newp = p; + } + } } else { - void* newmem = internal_malloc(m, bytes); - if (newmem != 0) { - size_t oc = oldsize - overhead_for(oldp); - memcpy(newmem, oldmem, (oc < bytes)? oc : bytes); - internal_free(m, oldmem); - } - return newmem; + USAGE_ERROR_ACTION(m, chunk2mem(p)); } - } - return 0; + return newp; } -/* --------------------------- memalign support -------------------------- */ - static void* internal_memalign(mstate m, size_t alignment, size_t bytes) { - if (alignment <= MALLOC_ALIGNMENT) /* Can just use malloc */ - return internal_malloc(m, bytes); - if (alignment < MIN_CHUNK_SIZE) /* must be at least a minimum chunk size */ - alignment = MIN_CHUNK_SIZE; - if ((alignment & (alignment-SIZE_T_ONE)) != 0) {/* Ensure a power of 2 */ - size_t a = MALLOC_ALIGNMENT << 1; - while (a < alignment) a <<= 1; - alignment = a; - } - - if (bytes >= MAX_REQUEST - alignment) { - if (m != 0) { /* Test isn't needed but avoids compiler warning */ - MALLOC_FAILURE_ACTION; + void* mem = 0; + if (alignment < MIN_CHUNK_SIZE) /* must be at least a minimum chunk size */ + alignment = MIN_CHUNK_SIZE; + if ((alignment & (alignment-SIZE_T_ONE)) != 0) {/* Ensure a power of 2 */ + size_t a = MALLOC_ALIGNMENT << 1; + while (a < alignment) a <<= 1; + alignment = a; } - } - else { - size_t nb = request2size(bytes); - size_t req = nb + alignment + MIN_CHUNK_SIZE - CHUNK_OVERHEAD; - char* mem = (char*)internal_malloc(m, req); - if (mem != 0) { - void* leader = 0; - void* trailer = 0; - mchunkptr p = mem2chunk(mem); - - if (PREACTION(m)) return 0; - if ((((size_t)(mem)) % alignment) != 0) { /* misaligned */ - /* - Find an aligned spot inside chunk. Since we need to give - back leading space in a chunk of at least MIN_CHUNK_SIZE, if - the first calculation places us at a spot with less than - MIN_CHUNK_SIZE leader, we can move to the next aligned spot. - We've allocated enough total room so that this is always - possible. - */ - char* br = (char*)mem2chunk((size_t)(((size_t)(mem + - alignment - - SIZE_T_ONE)) & - -alignment)); - char* pos = ((size_t)(br - (char*)(p)) >= MIN_CHUNK_SIZE)? - br : br+alignment; - mchunkptr newp = (mchunkptr)pos; - size_t leadsize = pos - (char*)(p); - size_t newsize = chunksize(p) - leadsize; - - if (is_mmapped(p)) { /* For mmapped chunks, just adjust offset */ - newp->prev_foot = p->prev_foot + leadsize; - newp->head = newsize; - } - else { /* Otherwise, give back leader, use the rest */ - set_inuse(m, newp, newsize); - set_inuse(m, p, leadsize); - leader = chunk2mem(p); + if (bytes >= MAX_REQUEST - alignment) { + if (m != 0) { /* Test isn't needed but avoids compiler warning */ + MALLOC_FAILURE_ACTION; } - p = newp; - } - - /* Give back spare room at the end */ - if (!is_mmapped(p)) { - size_t size = chunksize(p); - if (size > nb + MIN_CHUNK_SIZE) { - size_t remainder_size = size - nb; - mchunkptr remainder = chunk_plus_offset(p, nb); - set_inuse(m, p, nb); - set_inuse(m, remainder, remainder_size); - trailer = chunk2mem(remainder); + } + else { + size_t nb = request2size(bytes); + size_t req = nb + alignment + MIN_CHUNK_SIZE - CHUNK_OVERHEAD; + mem = internal_malloc(m, req); + if (mem != 0) { + mchunkptr p = mem2chunk(mem); + if (PREACTION(m)) + return 0; + if ((((size_t)(mem)) & (alignment - 1)) != 0) { /* misaligned */ + /* + Find an aligned spot inside chunk. Since we need to give + back leading space in a chunk of at least MIN_CHUNK_SIZE, if + the first calculation places us at a spot with less than + MIN_CHUNK_SIZE leader, we can move to the next aligned spot. + We've allocated enough total room so that this is always + possible. + */ + char* br = (char*)mem2chunk((size_t)(((size_t)((char*)mem + alignment - + SIZE_T_ONE)) & + -alignment)); + char* pos = ((size_t)(br - (char*)(p)) >= MIN_CHUNK_SIZE)? + br : br+alignment; + mchunkptr newp = (mchunkptr)pos; + size_t leadsize = pos - (char*)(p); + size_t newsize = chunksize(p) - leadsize; + + if (is_mmapped(p)) { /* For mmapped chunks, just adjust offset */ + newp->prev_foot = p->prev_foot + leadsize; + newp->head = newsize; + } + else { /* Otherwise, give back leader, use the rest */ + set_inuse(m, newp, newsize); + set_inuse(m, p, leadsize); + dispose_chunk(m, p, leadsize); + } + p = newp; + } + + /* Give back spare room at the end */ + if (!is_mmapped(p)) { + size_t size = chunksize(p); + if (size > nb + MIN_CHUNK_SIZE) { + size_t remainder_size = size - nb; + mchunkptr remainder = chunk_plus_offset(p, nb); + set_inuse(m, p, nb); + set_inuse(m, remainder, remainder_size); + dispose_chunk(m, remainder, remainder_size); + } + } + + mem = chunk2mem(p); + assert (chunksize(p) >= nb); + assert(((size_t)mem & (alignment - 1)) == 0); + check_inuse_chunk(m, p); + POSTACTION(m); } - } - - assert (chunksize(p) >= nb); - assert((((size_t)(chunk2mem(p))) % alignment) == 0); - check_inuse_chunk(m, p); - POSTACTION(m); - if (leader != 0) { - internal_free(m, leader); - } - if (trailer != 0) { - internal_free(m, trailer); - } - return chunk2mem(p); } - } - return 0; + return mem; } -/* ------------------------ comalloc/coalloc support --------------------- */ - +/* + Common support for independent_X routines, handling + all of the combinations that can result. + The opts arg has: + bit 0 set if all elements are same size (using sizes[0]) + bit 1 set if elements should be zeroed + */ static void** ialloc(mstate m, size_t n_elements, size_t* sizes, int opts, void* chunks[]) { - /* - This provides common support for independent_X routines, handling - all of the combinations that can result. - - The opts arg has: - bit 0 set if all elements are same size (using sizes[0]) - bit 1 set if elements should be zeroed - */ - - size_t element_size; /* chunksize of each element, if all same */ - size_t contents_size; /* total size of elements */ - size_t array_size; /* request size of pointer array */ - void* mem; /* malloced aggregate space */ - mchunkptr p; /* corresponding chunk */ - size_t remainder_size; /* remaining bytes while splitting */ - void** marray; /* either "chunks" or malloced ptr array */ - mchunkptr array_chunk; /* chunk for malloced ptr array */ - flag_t was_enabled; /* to disable mmap */ - size_t size; - size_t i; - - ensure_initialization(); - /* compute array length, if needed */ - if (chunks != 0) { - if (n_elements == 0) - return chunks; /* nothing to do */ - marray = chunks; - array_size = 0; - } - else { - /* if empty req, must still return chunk representing empty array */ - if (n_elements == 0) - return (void**)internal_malloc(m, 0); - marray = 0; - array_size = request2size(n_elements * (sizeof(void*))); - } - - /* compute total element size */ - if (opts & 0x1) { /* all-same-size */ - element_size = request2size(*sizes); - contents_size = n_elements * element_size; - } - else { /* add up all the sizes */ - element_size = 0; - contents_size = 0; - for (i = 0; i != n_elements; ++i) - contents_size += request2size(sizes[i]); - } - - size = contents_size + array_size; - - /* + + size_t element_size; /* chunksize of each element, if all same */ + size_t contents_size; /* total size of elements */ + size_t array_size; /* request size of pointer array */ + void* mem; /* malloced aggregate space */ + mchunkptr p; /* corresponding chunk */ + size_t remainder_size; /* remaining bytes while splitting */ + void** marray; /* either "chunks" or malloced ptr array */ + mchunkptr array_chunk; /* chunk for malloced ptr array */ + flag_t was_enabled; /* to disable mmap */ + size_t size; + size_t i; + + ensure_initialization(); + /* compute array length, if needed */ + if (chunks != 0) { + if (n_elements == 0) + return chunks; /* nothing to do */ + marray = chunks; + array_size = 0; + } + else { + /* if empty req, must still return chunk representing empty array */ + if (n_elements == 0) + return (void**)internal_malloc(m, 0); + marray = 0; + array_size = request2size(n_elements * (sizeof(void*))); + } + + /* compute total element size */ + if (opts & 0x1) { /* all-same-size */ + element_size = request2size(*sizes); + contents_size = n_elements * element_size; + } + else { /* add up all the sizes */ + element_size = 0; + contents_size = 0; + for (i = 0; i != n_elements; ++i) + contents_size += request2size(sizes[i]); + } + + size = contents_size + array_size; + + /* Allocate the aggregate chunk. First disable direct-mmapping so malloc won't use it, since we would not be able to later free/realloc space internal to a segregated mmap region. - */ - was_enabled = use_mmap(m); - disable_mmap(m); - mem = internal_malloc(m, size - CHUNK_OVERHEAD); - if (was_enabled) - enable_mmap(m); - if (mem == 0) - return 0; - - if (PREACTION(m)) return 0; - p = mem2chunk(mem); - remainder_size = chunksize(p); - - assert(!is_mmapped(p)); - - if (opts & 0x2) { /* optionally clear the elements */ - memset((size_t*)mem, 0, remainder_size - SIZE_T_SIZE - array_size); - } - - /* If not provided, allocate the pointer array as final part of chunk */ - if (marray == 0) { - size_t array_chunk_size; - array_chunk = chunk_plus_offset(p, contents_size); - array_chunk_size = remainder_size - contents_size; - marray = (void**) (chunk2mem(array_chunk)); - set_size_and_pinuse_of_inuse_chunk(m, array_chunk, array_chunk_size); - remainder_size = contents_size; - } - - /* split out elements */ - for (i = 0; ; ++i) { - marray[i] = chunk2mem(p); - if (i != n_elements-1) { - if (element_size != 0) - size = element_size; - else - size = request2size(sizes[i]); - remainder_size -= size; - set_size_and_pinuse_of_inuse_chunk(m, p, size); - p = chunk_plus_offset(p, size); + */ + was_enabled = use_mmap(m); + disable_mmap(m); + mem = internal_malloc(m, size - CHUNK_OVERHEAD); + if (was_enabled) + enable_mmap(m); + if (mem == 0) + return 0; + + if (PREACTION(m)) return 0; + p = mem2chunk(mem); + remainder_size = chunksize(p); + + assert(!is_mmapped(p)); + + if (opts & 0x2) { /* optionally clear the elements */ + memset((size_t*)mem, 0, remainder_size - SIZE_T_SIZE - array_size); } - else { /* the final element absorbs any overallocation slop */ - set_size_and_pinuse_of_inuse_chunk(m, p, remainder_size); - break; + + /* If not provided, allocate the pointer array as final part of chunk */ + if (marray == 0) { + size_t array_chunk_size; + array_chunk = chunk_plus_offset(p, contents_size); + array_chunk_size = remainder_size - contents_size; + marray = (void**) (chunk2mem(array_chunk)); + set_size_and_pinuse_of_inuse_chunk(m, array_chunk, array_chunk_size); + remainder_size = contents_size; } - } - -#if DEBUG - if (marray != chunks) { - /* final element must have exactly exhausted chunk */ - if (element_size != 0) { - assert(remainder_size == element_size); + + /* split out elements */ + for (i = 0; ; ++i) { + marray[i] = chunk2mem(p); + if (i != n_elements-1) { + if (element_size != 0) + size = element_size; + else + size = request2size(sizes[i]); + remainder_size -= size; + set_size_and_pinuse_of_inuse_chunk(m, p, size); + p = chunk_plus_offset(p, size); + } + else { /* the final element absorbs any overallocation slop */ + set_size_and_pinuse_of_inuse_chunk(m, p, remainder_size); + break; + } } - else { - assert(remainder_size == request2size(sizes[i])); + +#if DEBUG + if (marray != chunks) { + /* final element must have exactly exhausted chunk */ + if (element_size != 0) { + assert(remainder_size == element_size); + } + else { + assert(remainder_size == request2size(sizes[i])); + } + check_inuse_chunk(m, mem2chunk(marray)); } - check_inuse_chunk(m, mem2chunk(marray)); - } - for (i = 0; i != n_elements; ++i) - check_inuse_chunk(m, mem2chunk(marray[i])); - + for (i = 0; i != n_elements; ++i) + check_inuse_chunk(m, mem2chunk(marray[i])); + #endif /* DEBUG */ - - POSTACTION(m); - return marray; + + POSTACTION(m); + return marray; } - -/* -------------------------- public routines ---------------------------- */ - -#if !ONLY_MSPACES - -void* dlmalloc(size_t bytes) { - /* - Basic algorithm: - If a small request (< 256 bytes minus per-chunk overhead): - 1. If one exists, use a remainderless chunk in associated smallbin. - (Remainderless means that there are too few excess bytes to - represent as a chunk.) - 2. If it is big enough, use the dv chunk, which is normally the - chunk adjacent to the one used for the most recent small request. - 3. If one exists, split the smallest available chunk in a bin, - saving remainder in dv. - 4. If it is big enough, use the top chunk. - 5. If available, get memory from system and use it - Otherwise, for a large request: - 1. Find the smallest available binned chunk that fits, and use it - if it is better fitting than dv chunk, splitting if necessary. - 2. If better fitting than any binned chunk, use the dv chunk. - 3. If it is big enough, use the top chunk. - 4. If request size >= mmap threshold, try to directly mmap this chunk. - 5. If available, get memory from system and use it - - The ugly goto's here ensure that postaction occurs along all paths. - */ - -#if USE_LOCKS - ensure_initialization(); /* initialize in sys_alloc if not using locks */ +/* Try to free all pointers in the given array. + Note: this could be made faster, by delaying consolidation, + at the price of disabling some user integrity checks, We + still optimize some consolidations by combining adjacent + chunks before freeing, which will occur often if allocated + with ialloc or the array is sorted. + */ +static size_t internal_bulk_free(mstate m, void* array[], size_t nelem) { + size_t unfreed = 0; + if (!PREACTION(m)) { + void** a; + void** fence = &(array[nelem]); + for (a = array; a != fence; ++a) { + void* mem = *a; + if (mem != 0) { + mchunkptr p = mem2chunk(mem); + size_t psize = chunksize(p); +#if FOOTERS + if (get_mstate_for(p) != m) { + ++unfreed; + continue; + } #endif - - if (!PREACTION(gm)) { - void* mem; - size_t nb; - if (bytes <= MAX_SMALL_REQUEST) { - bindex_t idx; - binmap_t smallbits; - nb = (bytes < MIN_REQUEST)? MIN_CHUNK_SIZE : pad_request(bytes); - idx = small_index(nb); - smallbits = gm->smallmap >> idx; - - if ((smallbits & 0x3U) != 0) { /* Remainderless fit to a smallbin. */ - mchunkptr b, p; - idx += ~smallbits & 1; /* Uses next bin if idx empty */ - b = smallbin_at(gm, idx); - p = b->fd; - assert(chunksize(p) == small_index2size(idx)); - unlink_first_small_chunk(gm, b, p, idx); - set_inuse_and_pinuse(gm, p, small_index2size(idx)); - mem = chunk2mem(p); - check_malloced_chunk(gm, mem, nb); - goto postaction; - } - - else if (nb > gm->dvsize) { - if (smallbits != 0) { /* Use chunk in next nonempty smallbin */ - mchunkptr b, p, r; - size_t rsize; - bindex_t i; - binmap_t leftbits = (smallbits << idx) & left_bits(idx2bit(idx)); - binmap_t leastbit = least_bit(leftbits); - compute_bit2idx(leastbit, i); - b = smallbin_at(gm, i); - p = b->fd; - assert(chunksize(p) == small_index2size(i)); - unlink_first_small_chunk(gm, b, p, i); - rsize = small_index2size(i) - nb; - /* Fit here cannot be remainderless if 4byte sizes */ - if (SIZE_T_SIZE != 4 && rsize < MIN_CHUNK_SIZE) - set_inuse_and_pinuse(gm, p, small_index2size(i)); - else { - set_size_and_pinuse_of_inuse_chunk(gm, p, nb); - r = chunk_plus_offset(p, nb); - set_size_and_pinuse_of_free_chunk(r, rsize); - replace_dv(gm, r, rsize); - } - mem = chunk2mem(p); - check_malloced_chunk(gm, mem, nb); - goto postaction; - } - - else if (gm->treemap != 0 && (mem = tmalloc_small(gm, nb)) != 0) { - check_malloced_chunk(gm, mem, nb); - goto postaction; + check_inuse_chunk(m, p); + *a = 0; + if (RTCHECK(ok_address(m, p) && ok_inuse(p))) { + void ** b = a + 1; /* try to merge with next chunk */ + mchunkptr next = next_chunk(p); + if (b != fence && *b == chunk2mem(next)) { + size_t newsize = chunksize(next) + psize; + set_inuse(m, p, newsize); + *b = chunk2mem(p); + } + else + dispose_chunk(m, p, psize); + } + else { + CORRUPTION_ERROR_ACTION(m); + break; + } + } } - } - } - else if (bytes >= MAX_REQUEST) - nb = MAX_SIZE_T; /* Too big to allocate. Force failure (in sys alloc) */ - else { - nb = pad_request(bytes); - if (gm->treemap != 0 && (mem = tmalloc_large(gm, nb)) != 0) { - check_malloced_chunk(gm, mem, nb); - goto postaction; - } - } - - if (nb <= gm->dvsize) { - size_t rsize = gm->dvsize - nb; - mchunkptr p = gm->dv; - if (rsize >= MIN_CHUNK_SIZE) { /* split dv */ - mchunkptr r = gm->dv = chunk_plus_offset(p, nb); - gm->dvsize = rsize; - set_size_and_pinuse_of_free_chunk(r, rsize); - set_size_and_pinuse_of_inuse_chunk(gm, p, nb); - } - else { /* exhaust dv */ - size_t dvs = gm->dvsize; - gm->dvsize = 0; - gm->dv = 0; - set_inuse_and_pinuse(gm, p, dvs); - } - mem = chunk2mem(p); - check_malloced_chunk(gm, mem, nb); - goto postaction; + if (should_trim(m, m->topsize)) + sys_trim(m, 0); + POSTACTION(m); } + return unfreed; +} - else if (nb < gm->topsize) { /* Split top */ - size_t rsize = gm->topsize -= nb; - mchunkptr p = gm->top; - mchunkptr r = gm->top = chunk_plus_offset(p, nb); - r->head = rsize | PINUSE_BIT; - set_size_and_pinuse_of_inuse_chunk(gm, p, nb); - mem = chunk2mem(p); - check_top_chunk(gm, gm->top); - check_malloced_chunk(gm, mem, nb); - goto postaction; +/* Traversal */ +#if MALLOC_INSPECT_ALL +static void internal_inspect_all(mstate m, + void(*handler)(void *start, + void *end, + size_t used_bytes, + void* callback_arg), + void* arg) { + if (is_initialized(m)) { + mchunkptr top = m->top; + msegmentptr s; + for (s = &m->seg; s != 0; s = s->next) { + mchunkptr q = align_as_chunk(s->base); + while (segment_holds(s, q) && q->head != FENCEPOST_HEAD) { + mchunkptr next = next_chunk(q); + size_t sz = chunksize(q); + size_t used; + void* start; + if (is_inuse(q)) { + used = sz - CHUNK_OVERHEAD; /* must not be mmapped */ + start = chunk2mem(q); + } + else { + used = 0; + if (is_small(sz)) { /* offset by possible bookkeeping */ + start = (void*)((char*)q + sizeof(struct malloc_chunk)); + } + else { + start = (void*)((char*)q + sizeof(struct malloc_tree_chunk)); + } + } + if (start < (void*)next) /* skip if all space is bookkeeping */ + handler(start, next, used, arg); + if (q == top) + break; + q = next; + } + } } - - mem = sys_alloc(gm, nb); - - postaction: - POSTACTION(gm); - return mem; - } - - return 0; } +#endif /* MALLOC_INSPECT_ALL */ -void dlfree(void* mem) { - /* - Consolidate freed chunks with preceeding or succeeding bordering - free chunks, if they exist, and then place in a bin. Intermixed - with special cases for top, dv, mmapped chunks, and usage errors. - */ +/* ------------------ Exported realloc, memalign, etc -------------------- */ - if (mem != 0) { - mchunkptr p = mem2chunk(mem); -#if FOOTERS - mstate fm = get_mstate_for(p); - if (!ok_magic(fm)) { - USAGE_ERROR_ACTION(fm, p); - return; +#if !ONLY_MSPACES + +void* dlrealloc(void* oldmem, size_t bytes) { + void* mem = 0; + if (oldmem == 0) { + mem = dlmalloc(bytes); + } + else if (bytes >= MAX_REQUEST) { + MALLOC_FAILURE_ACTION; + } +#ifdef REALLOC_ZERO_BYTES_FREES + else if (bytes == 0) { + dlfree(oldmem); } +#endif /* REALLOC_ZERO_BYTES_FREES */ + else { + size_t nb = request2size(bytes); + mchunkptr oldp = mem2chunk(oldmem); +#if ! FOOTERS + mstate m = gm; #else /* FOOTERS */ -#define fm gm + mstate m = get_mstate_for(oldp); + if (!ok_magic(m)) { + USAGE_ERROR_ACTION(m, oldmem); + return 0; + } #endif /* FOOTERS */ - if (!PREACTION(fm)) { - check_inuse_chunk(fm, p); - if (RTCHECK(ok_address(fm, p) && ok_inuse(p))) { - size_t psize = chunksize(p); - mchunkptr next = chunk_plus_offset(p, psize); - if (!pinuse(p)) { - size_t prevsize = p->prev_foot; - if (is_mmapped(p)) { - psize += prevsize + MMAP_FOOT_PAD; - if (CALL_MUNMAP((char*)p - prevsize, psize) == 0) - fm->footprint -= psize; - goto postaction; - } - else { - mchunkptr prev = chunk_minus_offset(p, prevsize); - psize += prevsize; - p = prev; - if (RTCHECK(ok_address(fm, prev))) { /* consolidate backward */ - if (p != fm->dv) { - unlink_chunk(fm, p, prevsize); - } - else if ((next->head & INUSE_BITS) == INUSE_BITS) { - fm->dvsize = psize; - set_free_with_pinuse(p, psize, next); - goto postaction; - } + if (!PREACTION(m)) { + mchunkptr newp = try_realloc_chunk(m, oldp, nb, 1); + POSTACTION(m); + if (newp != 0) { + check_inuse_chunk(m, newp); + mem = chunk2mem(newp); + } + else { + mem = internal_malloc(m, bytes); + if (mem != 0) { + size_t oc = chunksize(oldp) - overhead_for(oldp); + memcpy(mem, oldmem, (oc < bytes)? oc : bytes); + internal_free(m, oldmem); + } } - else - goto erroraction; - } } + } + return mem; +} - if (RTCHECK(ok_next(p, next) && ok_pinuse(next))) { - if (!cinuse(next)) { /* consolidate forward */ - if (next == fm->top) { - size_t tsize = fm->topsize += psize; - fm->top = p; - p->head = tsize | PINUSE_BIT; - if (p == fm->dv) { - fm->dv = 0; - fm->dvsize = 0; - } - if (should_trim(fm, tsize)) - sys_trim(fm, 0); - goto postaction; - } - else if (next == fm->dv) { - size_t dsize = fm->dvsize += psize; - fm->dv = p; - set_size_and_pinuse_of_free_chunk(p, dsize); - goto postaction; +void* dlrealloc_in_place(void* oldmem, size_t bytes) { + void* mem = 0; + if (oldmem != 0) { + if (bytes >= MAX_REQUEST) { + MALLOC_FAILURE_ACTION; + } + else { + size_t nb = request2size(bytes); + mchunkptr oldp = mem2chunk(oldmem); +#if ! FOOTERS + mstate m = gm; +#else /* FOOTERS */ + mstate m = get_mstate_for(oldp); + if (!ok_magic(m)) { + USAGE_ERROR_ACTION(m, oldmem); + return 0; } - else { - size_t nsize = chunksize(next); - psize += nsize; - unlink_chunk(fm, next, nsize); - set_size_and_pinuse_of_free_chunk(p, psize); - if (p == fm->dv) { - fm->dvsize = psize; - goto postaction; - } +#endif /* FOOTERS */ + if (!PREACTION(m)) { + mchunkptr newp = try_realloc_chunk(m, oldp, nb, 0); + POSTACTION(m); + if (newp == oldp) { + check_inuse_chunk(m, newp); + mem = oldmem; + } } - } - else - set_free_with_pinuse(p, psize, next); - - if (is_small(psize)) { - insert_small_chunk(fm, p, psize); - check_free_chunk(fm, p); - } - else { - tchunkptr tp = (tchunkptr)p; - insert_large_chunk(fm, tp, psize); - check_free_chunk(fm, p); - if (--fm->release_checks == 0) - release_unused_segments(fm); - } - goto postaction; } - } - erroraction: - USAGE_ERROR_ACTION(fm, p); - postaction: - POSTACTION(fm); } - } -#if !FOOTERS -#undef fm -#endif /* FOOTERS */ + return mem; } -void* dlcalloc(size_t n_elements, size_t elem_size) { - void* mem; - size_t req = 0; - if (n_elements != 0) { - req = n_elements * elem_size; - if (((n_elements | elem_size) & ~(size_t)0xffff) && - (req / n_elements != elem_size)) - req = MAX_SIZE_T; /* force downstream failure on overflow */ - } - mem = dlmalloc(req); - if (mem != 0 && calloc_must_clear(mem2chunk(mem))) - memset(mem, 0, req); - return mem; +void* dlmemalign(size_t alignment, size_t bytes) { + if (alignment <= MALLOC_ALIGNMENT) { + return dlmalloc(bytes); + } + return internal_memalign(gm, alignment, bytes); } -void* dlrealloc(void* oldmem, size_t bytes) { - if (oldmem == 0) - return dlmalloc(bytes); -#ifdef REALLOC_ZERO_BYTES_FREES - if (bytes == 0) { - dlfree(oldmem); - return 0; - } -#endif /* REALLOC_ZERO_BYTES_FREES */ - else { -#if ! FOOTERS - mstate m = gm; -#else /* FOOTERS */ - mstate m = get_mstate_for(mem2chunk(oldmem)); - if (!ok_magic(m)) { - USAGE_ERROR_ACTION(m, oldmem); - return 0; +int dlposix_memalign(void** pp, size_t alignment, size_t bytes) { + void* mem = 0; + if (alignment == MALLOC_ALIGNMENT) + mem = dlmalloc(bytes); + else { + size_t d = alignment / sizeof(void*); + size_t r = alignment % sizeof(void*); + if (r != 0 || d == 0 || (d & (d-SIZE_T_ONE)) != 0) + return EINVAL; + else if (bytes <= MAX_REQUEST - alignment) { + if (alignment < MIN_CHUNK_SIZE) + alignment = MIN_CHUNK_SIZE; + mem = internal_memalign(gm, alignment, bytes); + } + } + if (mem == 0) + return ENOMEM; + else { + *pp = mem; + return 0; } -#endif /* FOOTERS */ - return internal_realloc(m, oldmem, bytes); - } } -void* dlmemalign(size_t alignment, size_t bytes) { - return internal_memalign(gm, alignment, bytes); +void* dlvalloc(size_t bytes) { + size_t pagesz; + ensure_initialization(); + pagesz = mparams.page_size; + return dlmemalign(pagesz, bytes); +} + +void* dlpvalloc(size_t bytes) { + size_t pagesz; + ensure_initialization(); + pagesz = mparams.page_size; + return dlmemalign(pagesz, (bytes + pagesz - SIZE_T_ONE) & ~(pagesz - SIZE_T_ONE)); } void** dlindependent_calloc(size_t n_elements, size_t elem_size, - void* chunks[]) { - size_t sz = elem_size; /* serves as 1-element array */ - return ialloc(gm, n_elements, &sz, 3, chunks); + void* chunks[]) { + size_t sz = elem_size; /* serves as 1-element array */ + return ialloc(gm, n_elements, &sz, 3, chunks); } void** dlindependent_comalloc(size_t n_elements, size_t sizes[], - void* chunks[]) { - return ialloc(gm, n_elements, sizes, 0, chunks); + void* chunks[]) { + return ialloc(gm, n_elements, sizes, 0, chunks); } -void* dlvalloc(size_t bytes) { - size_t pagesz; - ensure_initialization(); - pagesz = mparams.page_size; - return dlmemalign(pagesz, bytes); +size_t dlbulk_free(void* array[], size_t nelem) { + return internal_bulk_free(gm, array, nelem); } -void* dlpvalloc(size_t bytes) { - size_t pagesz; - ensure_initialization(); - pagesz = mparams.page_size; - return dlmemalign(pagesz, (bytes + pagesz - SIZE_T_ONE) & ~(pagesz - SIZE_T_ONE)); +#if MALLOC_INSPECT_ALL +void dlmalloc_inspect_all(void(*handler)(void *start, + void *end, + size_t used_bytes, + void* callback_arg), + void* arg) { + ensure_initialization(); + if (!PREACTION(gm)) { + internal_inspect_all(gm, handler, arg); + POSTACTION(gm); + } } +#endif /* MALLOC_INSPECT_ALL */ int dlmalloc_trim(size_t pad) { - int result = 0; - ensure_initialization(); - if (!PREACTION(gm)) { - result = sys_trim(gm, pad); - POSTACTION(gm); - } - return result; + int result = 0; + ensure_initialization(); + if (!PREACTION(gm)) { + result = sys_trim(gm, pad); + POSTACTION(gm); + } + return result; } size_t dlmalloc_footprint(void) { - return gm->footprint; + return gm->footprint; } size_t dlmalloc_max_footprint(void) { - return gm->max_footprint; + return gm->max_footprint; +} + +size_t dlmalloc_footprint_limit(void) { + size_t maf = gm->footprint_limit; + return maf == 0 ? MAX_SIZE_T : maf; +} + +size_t dlmalloc_set_footprint_limit(size_t bytes) { + size_t result; /* invert sense of 0 */ + if (bytes == 0) + result = granularity_align(1); /* Use minimal size */ + if (bytes == MAX_SIZE_T) + result = 0; /* disable */ + else + result = granularity_align(bytes); + return gm->footprint_limit = result; } #if !NO_MALLINFO struct mallinfo dlmallinfo(void) { - return internal_mallinfo(gm); + return internal_mallinfo(gm); } #endif /* NO_MALLINFO */ +#if !NO_MALLOC_STATS void dlmalloc_stats() { - internal_malloc_stats(gm); + internal_malloc_stats(gm); } +#endif /* NO_MALLOC_STATS */ int dlmallopt(int param_number, int value) { - return change_mparam(param_number, value); + return change_mparam(param_number, value); } -#endif /* !ONLY_MSPACES */ - size_t dlmalloc_usable_size(void* mem) { - if (mem != 0) { - mchunkptr p = mem2chunk(mem); - if (is_inuse(p)) - return chunksize(p) - overhead_for(p); - } - return 0; + if (mem != 0) { + mchunkptr p = mem2chunk(mem); + if (is_inuse(p)) + return chunksize(p) - overhead_for(p); + } + return 0; } +#endif /* !ONLY_MSPACES */ + /* ----------------------------- user mspaces ---------------------------- */ #if MSPACES static mstate init_user_mstate(char* tbase, size_t tsize) { - size_t msize = pad_request(sizeof(struct malloc_state)); - mchunkptr mn; - mchunkptr msp = align_as_chunk(tbase); - mstate m = (mstate)(chunk2mem(msp)); - memset(m, 0, msize); - INITIAL_LOCK(&m->mutex); - msp->head = (msize|INUSE_BITS); - m->seg.base = m->least_addr = tbase; - m->seg.size = m->footprint = m->max_footprint = tsize; - m->magic = mparams.magic; - m->release_checks = MAX_RELEASE_CHECK_RATE; - m->mflags = mparams.default_mflags; - m->extp = 0; - m->exts = 0; - disable_contiguous(m); - init_bins(m); - mn = next_chunk(mem2chunk(m)); - init_top(m, mn, (size_t)((tbase + tsize) - (char*)mn) - TOP_FOOT_SIZE); - check_top_chunk(m, m->top); - return m; + size_t msize = pad_request(sizeof(struct malloc_state)); + mchunkptr mn; + mchunkptr msp = align_as_chunk(tbase); + mstate m = (mstate)(chunk2mem(msp)); + memset(m, 0, msize); + (void)INITIAL_LOCK(&m->mutex); + msp->head = (msize|INUSE_BITS); + m->seg.base = m->least_addr = tbase; + m->seg.size = m->footprint = m->max_footprint = tsize; + m->magic = mparams.magic; + m->release_checks = MAX_RELEASE_CHECK_RATE; + m->mflags = mparams.default_mflags; + m->extp = 0; + m->exts = 0; + disable_contiguous(m); + init_bins(m); + mn = next_chunk(mem2chunk(m)); + init_top(m, mn, (size_t)((tbase + tsize) - (char*)mn) - TOP_FOOT_SIZE); + check_top_chunk(m, m->top); + return m; } mspace create_mspace(size_t capacity, int locked) { - mstate m = 0; - size_t msize; - ensure_initialization(); - msize = pad_request(sizeof(struct malloc_state)); - if (capacity < (size_t) -(msize + TOP_FOOT_SIZE + mparams.page_size)) { - size_t rs = ((capacity == 0)? mparams.granularity : - (capacity + TOP_FOOT_SIZE + msize)); - size_t tsize = granularity_align(rs); - char* tbase = (char*)(CALL_MMAP(tsize)); - if (tbase != CMFAIL) { - m = init_user_mstate(tbase, tsize); - m->seg.sflags = USE_MMAP_BIT; - set_lock(m, locked); + mstate m = 0; + size_t msize; + ensure_initialization(); + msize = pad_request(sizeof(struct malloc_state)); + if (capacity < (size_t) -(msize + TOP_FOOT_SIZE + mparams.page_size)) { + size_t rs = ((capacity == 0)? mparams.granularity : + (capacity + TOP_FOOT_SIZE + msize)); + size_t tsize = granularity_align(rs); + char* tbase = (char*)(CALL_MMAP(tsize)); + if (tbase != CMFAIL) { + m = init_user_mstate(tbase, tsize); + m->seg.sflags = USE_MMAP_BIT; + set_lock(m, locked); + } } - } - return (mspace)m; + return (mspace)m; } mspace create_mspace_with_base(void* base, size_t capacity, int locked) { - mstate m = 0; - size_t msize; - ensure_initialization(); - msize = pad_request(sizeof(struct malloc_state)); - if (capacity > msize + TOP_FOOT_SIZE && - capacity < (size_t) -(msize + TOP_FOOT_SIZE + mparams.page_size)) { - m = init_user_mstate((char*)base, capacity); - m->seg.sflags = EXTERN_BIT; - set_lock(m, locked); - } - return (mspace)m; + mstate m = 0; + size_t msize; + ensure_initialization(); + msize = pad_request(sizeof(struct malloc_state)); + if (capacity > msize + TOP_FOOT_SIZE && + capacity < (size_t) -(msize + TOP_FOOT_SIZE + mparams.page_size)) { + m = init_user_mstate((char*)base, capacity); + m->seg.sflags = EXTERN_BIT; + set_lock(m, locked); + } + return (mspace)m; } int mspace_track_large_chunks(mspace msp, int enable) { - int ret = 0; - mstate ms = (mstate)msp; - if (!PREACTION(ms)) { - if (!use_mmap(ms)) - ret = 1; - if (!enable) - enable_mmap(ms); - else - disable_mmap(ms); - POSTACTION(ms); - } - return ret; + int ret = 0; + mstate ms = (mstate)msp; + if (!PREACTION(ms)) { + if (!use_mmap(ms)) { + ret = 1; + } + if (!enable) { + enable_mmap(ms); + } else { + disable_mmap(ms); + } + POSTACTION(ms); + } + return ret; } size_t destroy_mspace(mspace msp) { - size_t freed = 0; - mstate ms = (mstate)msp; - if (ok_magic(ms)) { - msegmentptr sp = &ms->seg; - while (sp != 0) { - char* base = sp->base; - size_t size = sp->size; - flag_t flag = sp->sflags; - sp = sp->next; - if ((flag & USE_MMAP_BIT) && !(flag & EXTERN_BIT) && - CALL_MUNMAP(base, size) == 0) - freed += size; + size_t freed = 0; + mstate ms = (mstate)msp; + if (ok_magic(ms)) { + msegmentptr sp = &ms->seg; + (void)DESTROY_LOCK(&ms->mutex); /* destroy before unmapped */ + while (sp != 0) { + char* base = sp->base; + size_t size = sp->size; + flag_t flag = sp->sflags; + (void)base; /* placate people compiling -Wunused-variable */ + sp = sp->next; + if ((flag & USE_MMAP_BIT) && !(flag & EXTERN_BIT) && + CALL_MUNMAP(base, size) == 0) + freed += size; + } } - } - else { - USAGE_ERROR_ACTION(ms,ms); - } - return freed; + else { + USAGE_ERROR_ACTION(ms,ms); + } + return freed; } /* - mspace versions of routines are near-clones of the global - versions. This is not so nice but better than the alternatives. -*/ - + mspace versions of routines are near-clones of the global + versions. This is not so nice but better than the alternatives. + */ void* mspace_malloc(mspace msp, size_t bytes) { - mstate ms = (mstate)msp; - if (!ok_magic(ms)) { - USAGE_ERROR_ACTION(ms,ms); - return 0; - } - if (!PREACTION(ms)) { - void* mem; - size_t nb; - if (bytes <= MAX_SMALL_REQUEST) { - bindex_t idx; - binmap_t smallbits; - nb = (bytes < MIN_REQUEST)? MIN_CHUNK_SIZE : pad_request(bytes); - idx = small_index(nb); - smallbits = ms->smallmap >> idx; - - if ((smallbits & 0x3U) != 0) { /* Remainderless fit to a smallbin. */ - mchunkptr b, p; - idx += ~smallbits & 1; /* Uses next bin if idx empty */ - b = smallbin_at(ms, idx); - p = b->fd; - assert(chunksize(p) == small_index2size(idx)); - unlink_first_small_chunk(ms, b, p, idx); - set_inuse_and_pinuse(ms, p, small_index2size(idx)); - mem = chunk2mem(p); - check_malloced_chunk(ms, mem, nb); - goto postaction; - } - - else if (nb > ms->dvsize) { - if (smallbits != 0) { /* Use chunk in next nonempty smallbin */ - mchunkptr b, p, r; - size_t rsize; - bindex_t i; - binmap_t leftbits = (smallbits << idx) & left_bits(idx2bit(idx)); - binmap_t leastbit = least_bit(leftbits); - compute_bit2idx(leastbit, i); - b = smallbin_at(ms, i); - p = b->fd; - assert(chunksize(p) == small_index2size(i)); - unlink_first_small_chunk(ms, b, p, i); - rsize = small_index2size(i) - nb; - /* Fit here cannot be remainderless if 4byte sizes */ - if (SIZE_T_SIZE != 4 && rsize < MIN_CHUNK_SIZE) - set_inuse_and_pinuse(ms, p, small_index2size(i)); - else { - set_size_and_pinuse_of_inuse_chunk(ms, p, nb); - r = chunk_plus_offset(p, nb); - set_size_and_pinuse_of_free_chunk(r, rsize); - replace_dv(ms, r, rsize); - } - mem = chunk2mem(p); - check_malloced_chunk(ms, mem, nb); - goto postaction; + mstate ms = (mstate)msp; + if (!ok_magic(ms)) { + USAGE_ERROR_ACTION(ms,ms); + return 0; + } + if (!PREACTION(ms)) { + void* mem; + size_t nb; + if (bytes <= MAX_SMALL_REQUEST) { + bindex_t idx; + binmap_t smallbits; + nb = (bytes < MIN_REQUEST)? MIN_CHUNK_SIZE : pad_request(bytes); + idx = small_index(nb); + smallbits = ms->smallmap >> idx; + + if ((smallbits & 0x3U) != 0) { /* Remainderless fit to a smallbin. */ + mchunkptr b, p; + idx += ~smallbits & 1; /* Uses next bin if idx empty */ + b = smallbin_at(ms, idx); + p = b->fd; + assert(chunksize(p) == small_index2size(idx)); + unlink_first_small_chunk(ms, b, p, idx); + set_inuse_and_pinuse(ms, p, small_index2size(idx)); + mem = chunk2mem(p); + check_malloced_chunk(ms, mem, nb); + goto postaction; + } + + else if (nb > ms->dvsize) { + if (smallbits != 0) { /* Use chunk in next nonempty smallbin */ + mchunkptr b, p, r; + size_t rsize; + bindex_t i; + binmap_t leftbits = (smallbits << idx) & left_bits(idx2bit(idx)); + binmap_t leastbit = least_bit(leftbits); + compute_bit2idx(leastbit, i); + b = smallbin_at(ms, i); + p = b->fd; + assert(chunksize(p) == small_index2size(i)); + unlink_first_small_chunk(ms, b, p, i); + rsize = small_index2size(i) - nb; + /* Fit here cannot be remainderless if 4byte sizes */ + if (SIZE_T_SIZE != 4 && rsize < MIN_CHUNK_SIZE) + set_inuse_and_pinuse(ms, p, small_index2size(i)); + else { + set_size_and_pinuse_of_inuse_chunk(ms, p, nb); + r = chunk_plus_offset(p, nb); + set_size_and_pinuse_of_free_chunk(r, rsize); + replace_dv(ms, r, rsize); + } + mem = chunk2mem(p); + check_malloced_chunk(ms, mem, nb); + goto postaction; + } + + else if (ms->treemap != 0 && (mem = tmalloc_small(ms, nb)) != 0) { + check_malloced_chunk(ms, mem, nb); + goto postaction; + } + } } - - else if (ms->treemap != 0 && (mem = tmalloc_small(ms, nb)) != 0) { - check_malloced_chunk(ms, mem, nb); - goto postaction; + else if (bytes >= MAX_REQUEST) + nb = MAX_SIZE_T; /* Too big to allocate. Force failure (in sys alloc) */ + else { + nb = pad_request(bytes); + if (ms->treemap != 0 && (mem = tmalloc_large(ms, nb)) != 0) { + check_malloced_chunk(ms, mem, nb); + goto postaction; + } } - } - } - else if (bytes >= MAX_REQUEST) - nb = MAX_SIZE_T; /* Too big to allocate. Force failure (in sys alloc) */ - else { - nb = pad_request(bytes); - if (ms->treemap != 0 && (mem = tmalloc_large(ms, nb)) != 0) { - check_malloced_chunk(ms, mem, nb); - goto postaction; - } - } - - if (nb <= ms->dvsize) { - size_t rsize = ms->dvsize - nb; - mchunkptr p = ms->dv; - if (rsize >= MIN_CHUNK_SIZE) { /* split dv */ - mchunkptr r = ms->dv = chunk_plus_offset(p, nb); - ms->dvsize = rsize; - set_size_and_pinuse_of_free_chunk(r, rsize); - set_size_and_pinuse_of_inuse_chunk(ms, p, nb); - } - else { /* exhaust dv */ - size_t dvs = ms->dvsize; - ms->dvsize = 0; - ms->dv = 0; - set_inuse_and_pinuse(ms, p, dvs); - } - mem = chunk2mem(p); - check_malloced_chunk(ms, mem, nb); - goto postaction; - } - - else if (nb < ms->topsize) { /* Split top */ - size_t rsize = ms->topsize -= nb; - mchunkptr p = ms->top; - mchunkptr r = ms->top = chunk_plus_offset(p, nb); - r->head = rsize | PINUSE_BIT; - set_size_and_pinuse_of_inuse_chunk(ms, p, nb); - mem = chunk2mem(p); - check_top_chunk(ms, ms->top); - check_malloced_chunk(ms, mem, nb); - goto postaction; + + if (nb <= ms->dvsize) { + size_t rsize = ms->dvsize - nb; + mchunkptr p = ms->dv; + if (rsize >= MIN_CHUNK_SIZE) { /* split dv */ + mchunkptr r = ms->dv = chunk_plus_offset(p, nb); + ms->dvsize = rsize; + set_size_and_pinuse_of_free_chunk(r, rsize); + set_size_and_pinuse_of_inuse_chunk(ms, p, nb); + } + else { /* exhaust dv */ + size_t dvs = ms->dvsize; + ms->dvsize = 0; + ms->dv = 0; + set_inuse_and_pinuse(ms, p, dvs); + } + mem = chunk2mem(p); + check_malloced_chunk(ms, mem, nb); + goto postaction; + } + + else if (nb < ms->topsize) { /* Split top */ + size_t rsize = ms->topsize -= nb; + mchunkptr p = ms->top; + mchunkptr r = ms->top = chunk_plus_offset(p, nb); + r->head = rsize | PINUSE_BIT; + set_size_and_pinuse_of_inuse_chunk(ms, p, nb); + mem = chunk2mem(p); + check_top_chunk(ms, ms->top); + check_malloced_chunk(ms, mem, nb); + goto postaction; + } + + mem = sys_alloc(ms, nb); + + postaction: + POSTACTION(ms); + return mem; } - - mem = sys_alloc(ms, nb); - - postaction: - POSTACTION(ms); - return mem; - } - - return 0; + + return 0; } void mspace_free(mspace msp, void* mem) { - if (mem != 0) { - mchunkptr p = mem2chunk(mem); + if (mem != 0) { + mchunkptr p = mem2chunk(mem); #if FOOTERS - mstate fm = get_mstate_for(p); - msp = msp; /* placate people compiling -Wunused */ + mstate fm = get_mstate_for(p); + (void)msp; /* placate people compiling -Wunused */ #else /* FOOTERS */ - mstate fm = (mstate)msp; + mstate fm = (mstate)msp; #endif /* FOOTERS */ - if (!ok_magic(fm)) { - USAGE_ERROR_ACTION(fm, p); - return; - } - if (!PREACTION(fm)) { - check_inuse_chunk(fm, p); - if (RTCHECK(ok_address(fm, p) && ok_inuse(p))) { - size_t psize = chunksize(p); - mchunkptr next = chunk_plus_offset(p, psize); - if (!pinuse(p)) { - size_t prevsize = p->prev_foot; - if (is_mmapped(p)) { - psize += prevsize + MMAP_FOOT_PAD; - if (CALL_MUNMAP((char*)p - prevsize, psize) == 0) - fm->footprint -= psize; - goto postaction; - } - else { - mchunkptr prev = chunk_minus_offset(p, prevsize); - psize += prevsize; - p = prev; - if (RTCHECK(ok_address(fm, prev))) { /* consolidate backward */ - if (p != fm->dv) { - unlink_chunk(fm, p, prevsize); - } - else if ((next->head & INUSE_BITS) == INUSE_BITS) { - fm->dvsize = psize; - set_free_with_pinuse(p, psize, next); - goto postaction; - } - } - else - goto erroraction; - } + if (!ok_magic(fm)) { + USAGE_ERROR_ACTION(fm, p); + return; } - - if (RTCHECK(ok_next(p, next) && ok_pinuse(next))) { - if (!cinuse(next)) { /* consolidate forward */ - if (next == fm->top) { - size_t tsize = fm->topsize += psize; - fm->top = p; - p->head = tsize | PINUSE_BIT; - if (p == fm->dv) { - fm->dv = 0; - fm->dvsize = 0; - } - if (should_trim(fm, tsize)) - sys_trim(fm, 0); - goto postaction; - } - else if (next == fm->dv) { - size_t dsize = fm->dvsize += psize; - fm->dv = p; - set_size_and_pinuse_of_free_chunk(p, dsize); - goto postaction; - } - else { - size_t nsize = chunksize(next); - psize += nsize; - unlink_chunk(fm, next, nsize); - set_size_and_pinuse_of_free_chunk(p, psize); - if (p == fm->dv) { - fm->dvsize = psize; - goto postaction; - } + if (!PREACTION(fm)) { + check_inuse_chunk(fm, p); + if (RTCHECK(ok_address(fm, p) && ok_inuse(p))) { + size_t psize = chunksize(p); + mchunkptr next = chunk_plus_offset(p, psize); + if (!pinuse(p)) { + size_t prevsize = p->prev_foot; + if (is_mmapped(p)) { + psize += prevsize + MMAP_FOOT_PAD; + if (CALL_MUNMAP((char*)p - prevsize, psize) == 0) + fm->footprint -= psize; + goto postaction; + } + else { + mchunkptr prev = chunk_minus_offset(p, prevsize); + psize += prevsize; + p = prev; + if (RTCHECK(ok_address(fm, prev))) { /* consolidate backward */ + if (p != fm->dv) { + unlink_chunk(fm, p, prevsize); + } + else if ((next->head & INUSE_BITS) == INUSE_BITS) { + fm->dvsize = psize; + set_free_with_pinuse(p, psize, next); + goto postaction; + } + } + else + goto erroraction; + } + } + + if (RTCHECK(ok_next(p, next) && ok_pinuse(next))) { + if (!cinuse(next)) { /* consolidate forward */ + if (next == fm->top) { + size_t tsize = fm->topsize += psize; + fm->top = p; + p->head = tsize | PINUSE_BIT; + if (p == fm->dv) { + fm->dv = 0; + fm->dvsize = 0; + } + if (should_trim(fm, tsize)) + sys_trim(fm, 0); + goto postaction; + } + else if (next == fm->dv) { + size_t dsize = fm->dvsize += psize; + fm->dv = p; + set_size_and_pinuse_of_free_chunk(p, dsize); + goto postaction; + } + else { + size_t nsize = chunksize(next); + psize += nsize; + unlink_chunk(fm, next, nsize); + set_size_and_pinuse_of_free_chunk(p, psize); + if (p == fm->dv) { + fm->dvsize = psize; + goto postaction; + } + } + } + else + set_free_with_pinuse(p, psize, next); + + if (is_small(psize)) { + insert_small_chunk(fm, p, psize); + check_free_chunk(fm, p); + } + else { + tchunkptr tp = (tchunkptr)p; + insert_large_chunk(fm, tp, psize); + check_free_chunk(fm, p); + if (--fm->release_checks == 0) + release_unused_segments(fm); + } + goto postaction; + } } - } - else - set_free_with_pinuse(p, psize, next); - - if (is_small(psize)) { - insert_small_chunk(fm, p, psize); - check_free_chunk(fm, p); - } - else { - tchunkptr tp = (tchunkptr)p; - insert_large_chunk(fm, tp, psize); - check_free_chunk(fm, p); - if (--fm->release_checks == 0) - release_unused_segments(fm); - } - goto postaction; + erroraction: + USAGE_ERROR_ACTION(fm, p); + postaction: + POSTACTION(fm); } - } - erroraction: - USAGE_ERROR_ACTION(fm, p); - postaction: - POSTACTION(fm); } - } } void* mspace_calloc(mspace msp, size_t n_elements, size_t elem_size) { - void* mem; - size_t req = 0; - mstate ms = (mstate)msp; - if (!ok_magic(ms)) { - USAGE_ERROR_ACTION(ms,ms); - return 0; - } - if (n_elements != 0) { - req = n_elements * elem_size; - if (((n_elements | elem_size) & ~(size_t)0xffff) && - (req / n_elements != elem_size)) - req = MAX_SIZE_T; /* force downstream failure on overflow */ - } - mem = internal_malloc(ms, req); - if (mem != 0 && calloc_must_clear(mem2chunk(mem))) - memset(mem, 0, req); - return mem; + void* mem; + size_t req = 0; + mstate ms = (mstate)msp; + if (!ok_magic(ms)) { + USAGE_ERROR_ACTION(ms,ms); + return 0; + } + if (n_elements != 0) { + req = n_elements * elem_size; + if (((n_elements | elem_size) & ~(size_t)0xffff) && + (req / n_elements != elem_size)) + req = MAX_SIZE_T; /* force downstream failure on overflow */ + } + mem = internal_malloc(ms, req); + if (mem != 0 && calloc_must_clear(mem2chunk(mem))) + memset(mem, 0, req); + return mem; } void* mspace_realloc(mspace msp, void* oldmem, size_t bytes) { - if (oldmem == 0) - return mspace_malloc(msp, bytes); + void* mem = 0; + if (oldmem == 0) { + mem = mspace_malloc(msp, bytes); + } + else if (bytes >= MAX_REQUEST) { + MALLOC_FAILURE_ACTION; + } #ifdef REALLOC_ZERO_BYTES_FREES - if (bytes == 0) { - mspace_free(msp, oldmem); - return 0; - } + else if (bytes == 0) { + mspace_free(msp, oldmem); + } #endif /* REALLOC_ZERO_BYTES_FREES */ - else { -#if FOOTERS - mchunkptr p = mem2chunk(oldmem); - mstate ms = get_mstate_for(p); + else { + size_t nb = request2size(bytes); + mchunkptr oldp = mem2chunk(oldmem); +#if ! FOOTERS + mstate m = (mstate)msp; #else /* FOOTERS */ - mstate ms = (mstate)msp; + mstate m = get_mstate_for(oldp); + if (!ok_magic(m)) { + USAGE_ERROR_ACTION(m, oldmem); + return 0; + } #endif /* FOOTERS */ - if (!ok_magic(ms)) { - USAGE_ERROR_ACTION(ms,ms); - return 0; + if (!PREACTION(m)) { + mchunkptr newp = try_realloc_chunk(m, oldp, nb, 1); + POSTACTION(m); + if (newp != 0) { + check_inuse_chunk(m, newp); + mem = chunk2mem(newp); + } + else { + mem = mspace_malloc(m, bytes); + if (mem != 0) { + size_t oc = chunksize(oldp) - overhead_for(oldp); + memcpy(mem, oldmem, (oc < bytes)? oc : bytes); + mspace_free(m, oldmem); + } + } + } } - return internal_realloc(ms, oldmem, bytes); - } + return mem; +} + +void* mspace_realloc_in_place(mspace msp, void* oldmem, size_t bytes) { + void* mem = 0; + if (oldmem != 0) { + if (bytes >= MAX_REQUEST) { + MALLOC_FAILURE_ACTION; + } + else { + size_t nb = request2size(bytes); + mchunkptr oldp = mem2chunk(oldmem); +#if ! FOOTERS + mstate m = (mstate)msp; +#else /* FOOTERS */ + mstate m = get_mstate_for(oldp); + (void)msp; /* placate people compiling -Wunused */ + if (!ok_magic(m)) { + USAGE_ERROR_ACTION(m, oldmem); + return 0; + } +#endif /* FOOTERS */ + if (!PREACTION(m)) { + mchunkptr newp = try_realloc_chunk(m, oldp, nb, 0); + POSTACTION(m); + if (newp == oldp) { + check_inuse_chunk(m, newp); + mem = oldmem; + } + } + } + } + return mem; } void* mspace_memalign(mspace msp, size_t alignment, size_t bytes) { - mstate ms = (mstate)msp; - if (!ok_magic(ms)) { - USAGE_ERROR_ACTION(ms,ms); - return 0; - } - return internal_memalign(ms, alignment, bytes); + mstate ms = (mstate)msp; + if (!ok_magic(ms)) { + USAGE_ERROR_ACTION(ms,ms); + return 0; + } + if (alignment <= MALLOC_ALIGNMENT) + return mspace_malloc(msp, bytes); + return internal_memalign(ms, alignment, bytes); } void** mspace_independent_calloc(mspace msp, size_t n_elements, size_t elem_size, void* chunks[]) { - size_t sz = elem_size; /* serves as 1-element array */ - mstate ms = (mstate)msp; - if (!ok_magic(ms)) { - USAGE_ERROR_ACTION(ms,ms); - return 0; - } - return ialloc(ms, n_elements, &sz, 3, chunks); + size_t sz = elem_size; /* serves as 1-element array */ + mstate ms = (mstate)msp; + if (!ok_magic(ms)) { + USAGE_ERROR_ACTION(ms,ms); + return 0; + } + return ialloc(ms, n_elements, &sz, 3, chunks); } void** mspace_independent_comalloc(mspace msp, size_t n_elements, size_t sizes[], void* chunks[]) { - mstate ms = (mstate)msp; - if (!ok_magic(ms)) { - USAGE_ERROR_ACTION(ms,ms); - return 0; - } - return ialloc(ms, n_elements, sizes, 0, chunks); + mstate ms = (mstate)msp; + if (!ok_magic(ms)) { + USAGE_ERROR_ACTION(ms,ms); + return 0; + } + return ialloc(ms, n_elements, sizes, 0, chunks); } +size_t mspace_bulk_free(mspace msp, void* array[], size_t nelem) { + return internal_bulk_free((mstate)msp, array, nelem); +} + +#if MALLOC_INSPECT_ALL +void mspace_inspect_all(mspace msp, + void(*handler)(void *start, + void *end, + size_t used_bytes, + void* callback_arg), + void* arg) { + mstate ms = (mstate)msp; + if (ok_magic(ms)) { + if (!PREACTION(ms)) { + internal_inspect_all(ms, handler, arg); + POSTACTION(ms); + } + } + else { + USAGE_ERROR_ACTION(ms,ms); + } +} +#endif /* MALLOC_INSPECT_ALL */ + int mspace_trim(mspace msp, size_t pad) { - int result = 0; - mstate ms = (mstate)msp; - if (ok_magic(ms)) { - if (!PREACTION(ms)) { - result = sys_trim(ms, pad); - POSTACTION(ms); + int result = 0; + mstate ms = (mstate)msp; + if (ok_magic(ms)) { + if (!PREACTION(ms)) { + result = sys_trim(ms, pad); + POSTACTION(ms); + } + } + else { + USAGE_ERROR_ACTION(ms,ms); } - } - else { - USAGE_ERROR_ACTION(ms,ms); - } - return result; + return result; } +#if !NO_MALLOC_STATS void mspace_malloc_stats(mspace msp) { - mstate ms = (mstate)msp; - if (ok_magic(ms)) { - internal_malloc_stats(ms); - } - else { - USAGE_ERROR_ACTION(ms,ms); - } + mstate ms = (mstate)msp; + if (ok_magic(ms)) { + internal_malloc_stats(ms); + } + else { + USAGE_ERROR_ACTION(ms,ms); + } } +#endif /* NO_MALLOC_STATS */ size_t mspace_footprint(mspace msp) { - size_t result = 0; - mstate ms = (mstate)msp; - if (ok_magic(ms)) { - result = ms->footprint; - } - else { - USAGE_ERROR_ACTION(ms,ms); - } - return result; + size_t result = 0; + mstate ms = (mstate)msp; + if (ok_magic(ms)) { + result = ms->footprint; + } + else { + USAGE_ERROR_ACTION(ms,ms); + } + return result; } - size_t mspace_max_footprint(mspace msp) { - size_t result = 0; - mstate ms = (mstate)msp; - if (ok_magic(ms)) { - result = ms->max_footprint; - } - else { - USAGE_ERROR_ACTION(ms,ms); - } - return result; + size_t result = 0; + mstate ms = (mstate)msp; + if (ok_magic(ms)) { + result = ms->max_footprint; + } + else { + USAGE_ERROR_ACTION(ms,ms); + } + return result; } +size_t mspace_footprint_limit(mspace msp) { + size_t result = 0; + mstate ms = (mstate)msp; + if (ok_magic(ms)) { + size_t maf = ms->footprint_limit; + result = (maf == 0) ? MAX_SIZE_T : maf; + } + else { + USAGE_ERROR_ACTION(ms,ms); + } + return result; +} + +size_t mspace_set_footprint_limit(mspace msp, size_t bytes) { + size_t result = 0; + mstate ms = (mstate)msp; + if (ok_magic(ms)) { + if (bytes == 0) + result = granularity_align(1); /* Use minimal size */ + if (bytes == MAX_SIZE_T) + result = 0; /* disable */ + else + result = granularity_align(bytes); + ms->footprint_limit = result; + } + else { + USAGE_ERROR_ACTION(ms,ms); + } + return result; +} #if !NO_MALLINFO struct mallinfo mspace_mallinfo(mspace msp) { - mstate ms = (mstate)msp; - if (!ok_magic(ms)) { - USAGE_ERROR_ACTION(ms,ms); - } - return internal_mallinfo(ms); + mstate ms = (mstate)msp; + if (!ok_magic(ms)) { + USAGE_ERROR_ACTION(ms,ms); + } + return internal_mallinfo(ms); } #endif /* NO_MALLINFO */ -size_t mspace_usable_size(void* mem) { - if (mem != 0) { - mchunkptr p = mem2chunk(mem); - if (is_inuse(p)) - return chunksize(p) - overhead_for(p); - } - return 0; +size_t mspace_usable_size(const void* mem) { + if (mem != 0) { + mchunkptr p = mem2chunk(mem); + if (is_inuse(p)) + return chunksize(p) - overhead_for(p); + } + return 0; } int mspace_mallopt(int param_number, int value) { - return change_mparam(param_number, value); + return change_mparam(param_number, value); } #endif /* MSPACES */ @@ -5421,287 +5985,310 @@ int mspace_mallopt(int param_number, int value) { /* -------------------- Alternative MORECORE functions ------------------- */ /* - Guidelines for creating a custom version of MORECORE: - - * For best performance, MORECORE should allocate in multiples of pagesize. - * MORECORE may allocate more memory than requested. (Or even less, - but this will usually result in a malloc failure.) - * MORECORE must not allocate memory when given argument zero, but - instead return one past the end address of memory from previous - nonzero call. - * For best performance, consecutive calls to MORECORE with positive - arguments should return increasing addresses, indicating that - space has been contiguously extended. - * Even though consecutive calls to MORECORE need not return contiguous - addresses, it must be OK for malloc'ed chunks to span multiple - regions in those cases where they do happen to be contiguous. - * MORECORE need not handle negative arguments -- it may instead - just return MFAIL when given negative arguments. - Negative arguments are always multiples of pagesize. MORECORE - must not misinterpret negative args as large positive unsigned - args. You can suppress all such calls from even occurring by defining - MORECORE_CANNOT_TRIM, - - As an example alternative MORECORE, here is a custom allocator - kindly contributed for pre-OSX macOS. It uses virtually but not - necessarily physically contiguous non-paged memory (locked in, - present and won't get swapped out). You can use it by uncommenting - this section, adding some #includes, and setting up the appropriate - defines above: - - #define MORECORE osMoreCore - - There is also a shutdown routine that should somehow be called for - cleanup upon program exit. - - #define MAX_POOL_ENTRIES 100 - #define MINIMUM_MORECORE_SIZE (64 * 1024U) - static int next_os_pool; - void *our_os_pools[MAX_POOL_ENTRIES]; - - void *osMoreCore(int size) - { - void *ptr = 0; - static void *sbrk_top = 0; - - if (size > 0) - { - if (size < MINIMUM_MORECORE_SIZE) - size = MINIMUM_MORECORE_SIZE; - if (CurrentExecutionLevel() == kTaskLevel) - ptr = PoolAllocateResident(size + RM_PAGE_SIZE, 0); - if (ptr == 0) - { - return (void *) MFAIL; - } - // save ptrs so they can be freed during cleanup - our_os_pools[next_os_pool] = ptr; - next_os_pool++; - ptr = (void *) ((((size_t) ptr) + RM_PAGE_MASK) & ~RM_PAGE_MASK); - sbrk_top = (char *) ptr + size; - return ptr; - } - else if (size < 0) - { - // we don't currently support shrink behavior - return (void *) MFAIL; - } - else - { - return sbrk_top; - } - } - - // cleanup any allocated memory pools - // called as last thing before shutting down driver - - void osCleanupMem(void) - { - void **ptr; - - for (ptr = our_os_pools; ptr < &our_os_pools[MAX_POOL_ENTRIES]; ptr++) - if (*ptr) - { - PoolDeallocate(*ptr); - *ptr = 0; - } - } - -*/ + Guidelines for creating a custom version of MORECORE: + + * For best performance, MORECORE should allocate in multiples of pagesize. + * MORECORE may allocate more memory than requested. (Or even less, + but this will usually result in a malloc failure.) + * MORECORE must not allocate memory when given argument zero, but + instead return one past the end address of memory from previous + nonzero call. + * For best performance, consecutive calls to MORECORE with positive + arguments should return increasing addresses, indicating that + space has been contiguously extended. + * Even though consecutive calls to MORECORE need not return contiguous + addresses, it must be OK for malloc'ed chunks to span multiple + regions in those cases where they do happen to be contiguous. + * MORECORE need not handle negative arguments -- it may instead + just return MFAIL when given negative arguments. + Negative arguments are always multiples of pagesize. MORECORE + must not misinterpret negative args as large positive unsigned + args. You can suppress all such calls from even occurring by defining + MORECORE_CANNOT_TRIM, + + As an example alternative MORECORE, here is a custom allocator + kindly contributed for pre-OSX macOS. It uses virtually but not + necessarily physically contiguous non-paged memory (locked in, + present and won't get swapped out). You can use it by uncommenting + this section, adding some #includes, and setting up the appropriate + defines above: + + #define MORECORE osMoreCore + + There is also a shutdown routine that should somehow be called for + cleanup upon program exit. + + #define MAX_POOL_ENTRIES 100 + #define MINIMUM_MORECORE_SIZE (64 * 1024U) + static int next_os_pool; + void *our_os_pools[MAX_POOL_ENTRIES]; + + void *osMoreCore(int size) + { + void *ptr = 0; + static void *sbrk_top = 0; + + if (size > 0) + { + if (size < MINIMUM_MORECORE_SIZE) + size = MINIMUM_MORECORE_SIZE; + if (CurrentExecutionLevel() == kTaskLevel) + ptr = PoolAllocateResident(size + RM_PAGE_SIZE, 0); + if (ptr == 0) + { + return (void *) MFAIL; + } + // save ptrs so they can be freed during cleanup + our_os_pools[next_os_pool] = ptr; + next_os_pool++; + ptr = (void *) ((((size_t) ptr) + RM_PAGE_MASK) & ~RM_PAGE_MASK); + sbrk_top = (char *) ptr + size; + return ptr; + } + else if (size < 0) + { + // we don't currently support shrink behavior + return (void *) MFAIL; + } + else + { + return sbrk_top; + } + } + + // cleanup any allocated memory pools + // called as last thing before shutting down driver + + void osCleanupMem(void) + { + void **ptr; + + for (ptr = our_os_pools; ptr < &our_os_pools[MAX_POOL_ENTRIES]; ptr++) + if (*ptr) + { + PoolDeallocate(*ptr); + *ptr = 0; + } + } + + */ /* ----------------------------------------------------------------------- -History: - V2.8.4 Wed May 27 09:56:23 2009 Doug Lea (dl at gee) - * Use zeros instead of prev foot for is_mmapped - * Add mspace_track_large_chunks; thanks to Jean Brouwers - * Fix set_inuse in internal_realloc; thanks to Jean Brouwers - * Fix insufficient sys_alloc padding when using 16byte alignment - * Fix bad error check in mspace_footprint - * Adaptations for ptmalloc; thanks to Wolfram Gloger. - * Reentrant spin locks; thanks to Earl Chew and others - * Win32 improvements; thanks to Niall Douglas and Earl Chew - * Add NO_SEGMENT_TRAVERSAL and MAX_RELEASE_CHECK_RATE options - * Extension hook in malloc_state - * Various small adjustments to reduce warnings on some compilers - * Various configuration extensions/changes for more platforms. Thanks - to all who contributed these. - - V2.8.3 Thu Sep 22 11:16:32 2005 Doug Lea (dl at gee) - * Add max_footprint functions - * Ensure all appropriate literals are size_t - * Fix conditional compilation problem for some #define settings - * Avoid concatenating segments with the one provided - in create_mspace_with_base - * Rename some variables to avoid compiler shadowing warnings - * Use explicit lock initialization. - * Better handling of sbrk interference. - * Simplify and fix segment insertion, trimming and mspace_destroy - * Reinstate REALLOC_ZERO_BYTES_FREES option from 2.7.x - * Thanks especially to Dennis Flanagan for help on these. - - V2.8.2 Sun Jun 12 16:01:10 2005 Doug Lea (dl at gee) - * Fix memalign brace error. - - V2.8.1 Wed Jun 8 16:11:46 2005 Doug Lea (dl at gee) - * Fix improper #endif nesting in C++ - * Add explicit casts needed for C++ - - V2.8.0 Mon May 30 14:09:02 2005 Doug Lea (dl at gee) - * Use trees for large bins - * Support mspaces - * Use segments to unify sbrk-based and mmap-based system allocation, - removing need for emulation on most platforms without sbrk. - * Default safety checks - * Optional footer checks. Thanks to William Robertson for the idea. - * Internal code refactoring - * Incorporate suggestions and platform-specific changes. - Thanks to Dennis Flanagan, Colin Plumb, Niall Douglas, - Aaron Bachmann, Emery Berger, and others. - * Speed up non-fastbin processing enough to remove fastbins. - * Remove useless cfree() to avoid conflicts with other apps. - * Remove internal memcpy, memset. Compilers handle builtins better. - * Remove some options that no one ever used and rename others. - - V2.7.2 Sat Aug 17 09:07:30 2002 Doug Lea (dl at gee) - * Fix malloc_state bitmap array misdeclaration - - V2.7.1 Thu Jul 25 10:58:03 2002 Doug Lea (dl at gee) - * Allow tuning of FIRST_SORTED_BIN_SIZE - * Use PTR_UINT as type for all ptr->int casts. Thanks to John Belmonte. - * Better detection and support for non-contiguousness of MORECORE. - Thanks to Andreas Mueller, Conal Walsh, and Wolfram Gloger - * Bypass most of malloc if no frees. Thanks To Emery Berger. - * Fix freeing of old top non-contiguous chunk im sysmalloc. - * Raised default trim and map thresholds to 256K. - * Fix mmap-related #defines. Thanks to Lubos Lunak. - * Fix copy macros; added LACKS_FCNTL_H. Thanks to Neal Walfield. - * Branch-free bin calculation - * Default trim and mmap thresholds now 256K. - - V2.7.0 Sun Mar 11 14:14:06 2001 Doug Lea (dl at gee) - * Introduce independent_comalloc and independent_calloc. - Thanks to Michael Pachos for motivation and help. - * Make optional .h file available - * Allow > 2GB requests on 32bit systems. - * new WIN32 sbrk, mmap, munmap, lock code from <Walter@GeNeSys-e.de>. - Thanks also to Andreas Mueller <a.mueller at paradatec.de>, - and Anonymous. - * Allow override of MALLOC_ALIGNMENT (Thanks to Ruud Waij for - helping test this.) - * memalign: check alignment arg - * realloc: don't try to shift chunks backwards, since this - leads to more fragmentation in some programs and doesn't - seem to help in any others. - * Collect all cases in malloc requiring system memory into sysmalloc - * Use mmap as backup to sbrk - * Place all internal state in malloc_state - * Introduce fastbins (although similar to 2.5.1) - * Many minor tunings and cosmetic improvements - * Introduce USE_PUBLIC_MALLOC_WRAPPERS, USE_MALLOC_LOCK - * Introduce MALLOC_FAILURE_ACTION, MORECORE_CONTIGUOUS - Thanks to Tony E. Bennett <tbennett@nvidia.com> and others. - * Include errno.h to support default failure action. - - V2.6.6 Sun Dec 5 07:42:19 1999 Doug Lea (dl at gee) - * return null for negative arguments - * Added Several WIN32 cleanups from Martin C. Fong <mcfong at yahoo.com> - * Add 'LACKS_SYS_PARAM_H' for those systems without 'sys/param.h' - (e.g. WIN32 platforms) - * Cleanup header file inclusion for WIN32 platforms - * Cleanup code to avoid Microsoft Visual C++ compiler complaints - * Add 'USE_DL_PREFIX' to quickly allow co-existence with existing - memory allocation routines - * Set 'malloc_getpagesize' for WIN32 platforms (needs more work) - * Use 'assert' rather than 'ASSERT' in WIN32 code to conform to - usage of 'assert' in non-WIN32 code - * Improve WIN32 'sbrk()' emulation's 'findRegion()' routine to - avoid infinite loop - * Always call 'fREe()' rather than 'free()' - - V2.6.5 Wed Jun 17 15:57:31 1998 Doug Lea (dl at gee) - * Fixed ordering problem with boundary-stamping - - V2.6.3 Sun May 19 08:17:58 1996 Doug Lea (dl at gee) - * Added pvalloc, as recommended by H.J. Liu - * Added 64bit pointer support mainly from Wolfram Gloger - * Added anonymously donated WIN32 sbrk emulation - * Malloc, calloc, getpagesize: add optimizations from Raymond Nijssen - * malloc_extend_top: fix mask error that caused wastage after - foreign sbrks - * Add linux mremap support code from HJ Liu - - V2.6.2 Tue Dec 5 06:52:55 1995 Doug Lea (dl at gee) - * Integrated most documentation with the code. - * Add support for mmap, with help from - Wolfram Gloger (Gloger@lrz.uni-muenchen.de). - * Use last_remainder in more cases. - * Pack bins using idea from colin@nyx10.cs.du.edu - * Use ordered bins instead of best-fit threshhold - * Eliminate block-local decls to simplify tracing and debugging. - * Support another case of realloc via move into top - * Fix error occuring when initial sbrk_base not word-aligned. - * Rely on page size for units instead of SBRK_UNIT to - avoid surprises about sbrk alignment conventions. - * Add mallinfo, mallopt. Thanks to Raymond Nijssen - (raymond@es.ele.tue.nl) for the suggestion. - * Add `pad' argument to malloc_trim and top_pad mallopt parameter. - * More precautions for cases where other routines call sbrk, - courtesy of Wolfram Gloger (Gloger@lrz.uni-muenchen.de). - * Added macros etc., allowing use in linux libc from - H.J. Lu (hjl@gnu.ai.mit.edu) - * Inverted this history list - - V2.6.1 Sat Dec 2 14:10:57 1995 Doug Lea (dl at gee) - * Re-tuned and fixed to behave more nicely with V2.6.0 changes. - * Removed all preallocation code since under current scheme - the work required to undo bad preallocations exceeds - the work saved in good cases for most test programs. - * No longer use return list or unconsolidated bins since - no scheme using them consistently outperforms those that don't - given above changes. - * Use best fit for very large chunks to prevent some worst-cases. - * Added some support for debugging - - V2.6.0 Sat Nov 4 07:05:23 1995 Doug Lea (dl at gee) - * Removed footers when chunks are in use. Thanks to - Paul Wilson (wilson@cs.texas.edu) for the suggestion. - - V2.5.4 Wed Nov 1 07:54:51 1995 Doug Lea (dl at gee) - * Added malloc_trim, with help from Wolfram Gloger - (wmglo@Dent.MED.Uni-Muenchen.DE). - - V2.5.3 Tue Apr 26 10:16:01 1994 Doug Lea (dl at g) - - V2.5.2 Tue Apr 5 16:20:40 1994 Doug Lea (dl at g) - * realloc: try to expand in both directions - * malloc: swap order of clean-bin strategy; - * realloc: only conditionally expand backwards - * Try not to scavenge used bins - * Use bin counts as a guide to preallocation - * Occasionally bin return list chunks in first scan - * Add a few optimizations from colin@nyx10.cs.du.edu - - V2.5.1 Sat Aug 14 15:40:43 1993 Doug Lea (dl at g) - * faster bin computation & slightly different binning - * merged all consolidations to one part of malloc proper - (eliminating old malloc_find_space & malloc_clean_bin) - * Scan 2 returns chunks (not just 1) - * Propagate failure in realloc if malloc returns 0 - * Add stuff to allow compilation on non-ANSI compilers - from kpv@research.att.com - - V2.5 Sat Aug 7 07:41:59 1993 Doug Lea (dl at g.oswego.edu) - * removed potential for odd address access in prev_chunk - * removed dependency on getpagesize.h - * misc cosmetics and a bit more internal documentation - * anticosmetics: mangled names in macros to evade debugger strangeness - * tested on sparc, hp-700, dec-mips, rs6000 - with gcc & native cc (hp, dec only) allowing - Detlefs & Zorn comparison study (in SIGPLAN Notices.) - - Trial version Fri Aug 28 13:14:29 1992 Doug Lea (dl at g.oswego.edu) - * Based loosely on libg++-1.2X malloc. (It retains some of the overall - structure of old version, but most details differ.) - -*/ + History: + v2.8.6 Wed Aug 29 06:57:58 2012 Doug Lea + * fix bad comparison in dlposix_memalign + * don't reuse adjusted asize in sys_alloc + * add LOCK_AT_FORK -- thanks to Kirill Artamonov for the suggestion + * reduce compiler warnings -- thanks to all who reported/suggested these + + v2.8.5 Sun May 22 10:26:02 2011 Doug Lea (dl at gee) + * Always perform unlink checks unless INSECURE + * Add posix_memalign. + * Improve realloc to expand in more cases; expose realloc_in_place. + Thanks to Peter Buhr for the suggestion. + * Add footprint_limit, inspect_all, bulk_free. Thanks + to Barry Hayes and others for the suggestions. + * Internal refactorings to avoid calls while holding locks + * Use non-reentrant locks by default. Thanks to Roland McGrath + for the suggestion. + * Small fixes to mspace_destroy, reset_on_error. + * Various configuration extensions/changes. Thanks + to all who contributed these. + + V2.8.4a Thu Apr 28 14:39:43 2011 (dl at gee.cs.oswego.edu) + * Update Creative Commons URL + + V2.8.4 Wed May 27 09:56:23 2009 Doug Lea (dl at gee) + * Use zeros instead of prev foot for is_mmapped + * Add mspace_track_large_chunks; thanks to Jean Brouwers + * Fix set_inuse in internal_realloc; thanks to Jean Brouwers + * Fix insufficient sys_alloc padding when using 16byte alignment + * Fix bad error check in mspace_footprint + * Adaptations for ptmalloc; thanks to Wolfram Gloger. + * Reentrant spin locks; thanks to Earl Chew and others + * Win32 improvements; thanks to Niall Douglas and Earl Chew + * Add NO_SEGMENT_TRAVERSAL and MAX_RELEASE_CHECK_RATE options + * Extension hook in malloc_state + * Various small adjustments to reduce warnings on some compilers + * Various configuration extensions/changes for more platforms. Thanks + to all who contributed these. + + V2.8.3 Thu Sep 22 11:16:32 2005 Doug Lea (dl at gee) + * Add max_footprint functions + * Ensure all appropriate literals are size_t + * Fix conditional compilation problem for some #define settings + * Avoid concatenating segments with the one provided + in create_mspace_with_base + * Rename some variables to avoid compiler shadowing warnings + * Use explicit lock initialization. + * Better handling of sbrk interference. + * Simplify and fix segment insertion, trimming and mspace_destroy + * Reinstate REALLOC_ZERO_BYTES_FREES option from 2.7.x + * Thanks especially to Dennis Flanagan for help on these. + + V2.8.2 Sun Jun 12 16:01:10 2005 Doug Lea (dl at gee) + * Fix memalign brace error. + + V2.8.1 Wed Jun 8 16:11:46 2005 Doug Lea (dl at gee) + * Fix improper #endif nesting in C++ + * Add explicit casts needed for C++ + + V2.8.0 Mon May 30 14:09:02 2005 Doug Lea (dl at gee) + * Use trees for large bins + * Support mspaces + * Use segments to unify sbrk-based and mmap-based system allocation, + removing need for emulation on most platforms without sbrk. + * Default safety checks + * Optional footer checks. Thanks to William Robertson for the idea. + * Internal code refactoring + * Incorporate suggestions and platform-specific changes. + Thanks to Dennis Flanagan, Colin Plumb, Niall Douglas, + Aaron Bachmann, Emery Berger, and others. + * Speed up non-fastbin processing enough to remove fastbins. + * Remove useless cfree() to avoid conflicts with other apps. + * Remove internal memcpy, memset. Compilers handle builtins better. + * Remove some options that no one ever used and rename others. + + V2.7.2 Sat Aug 17 09:07:30 2002 Doug Lea (dl at gee) + * Fix malloc_state bitmap array misdeclaration + + V2.7.1 Thu Jul 25 10:58:03 2002 Doug Lea (dl at gee) + * Allow tuning of FIRST_SORTED_BIN_SIZE + * Use PTR_UINT as type for all ptr->int casts. Thanks to John Belmonte. + * Better detection and support for non-contiguousness of MORECORE. + Thanks to Andreas Mueller, Conal Walsh, and Wolfram Gloger + * Bypass most of malloc if no frees. Thanks To Emery Berger. + * Fix freeing of old top non-contiguous chunk im sysmalloc. + * Raised default trim and map thresholds to 256K. + * Fix mmap-related #defines. Thanks to Lubos Lunak. + * Fix copy macros; added LACKS_FCNTL_H. Thanks to Neal Walfield. + * Branch-free bin calculation + * Default trim and mmap thresholds now 256K. + + V2.7.0 Sun Mar 11 14:14:06 2001 Doug Lea (dl at gee) + * Introduce independent_comalloc and independent_calloc. + Thanks to Michael Pachos for motivation and help. + * Make optional .h file available + * Allow > 2GB requests on 32bit systems. + * new WIN32 sbrk, mmap, munmap, lock code from <Walter@GeNeSys-e.de>. + Thanks also to Andreas Mueller <a.mueller at paradatec.de>, + and Anonymous. + * Allow override of MALLOC_ALIGNMENT (Thanks to Ruud Waij for + helping test this.) + * memalign: check alignment arg + * realloc: don't try to shift chunks backwards, since this + leads to more fragmentation in some programs and doesn't + seem to help in any others. + * Collect all cases in malloc requiring system memory into sysmalloc + * Use mmap as backup to sbrk + * Place all internal state in malloc_state + * Introduce fastbins (although similar to 2.5.1) + * Many minor tunings and cosmetic improvements + * Introduce USE_PUBLIC_MALLOC_WRAPPERS, USE_MALLOC_LOCK + * Introduce MALLOC_FAILURE_ACTION, MORECORE_CONTIGUOUS + Thanks to Tony E. Bennett <tbennett@nvidia.com> and others. + * Include errno.h to support default failure action. + + V2.6.6 Sun Dec 5 07:42:19 1999 Doug Lea (dl at gee) + * return null for negative arguments + * Added Several WIN32 cleanups from Martin C. Fong <mcfong at yahoo.com> + * Add 'LACKS_SYS_PARAM_H' for those systems without 'sys/param.h' + (e.g. WIN32 platforms) + * Cleanup header file inclusion for WIN32 platforms + * Cleanup code to avoid Microsoft Visual C++ compiler complaints + * Add 'USE_DL_PREFIX' to quickly allow co-existence with existing + memory allocation routines + * Set 'malloc_getpagesize' for WIN32 platforms (needs more work) + * Use 'assert' rather than 'ASSERT' in WIN32 code to conform to + usage of 'assert' in non-WIN32 code + * Improve WIN32 'sbrk()' emulation's 'findRegion()' routine to + avoid infinite loop + * Always call 'fREe()' rather than 'free()' + + V2.6.5 Wed Jun 17 15:57:31 1998 Doug Lea (dl at gee) + * Fixed ordering problem with boundary-stamping + + V2.6.3 Sun May 19 08:17:58 1996 Doug Lea (dl at gee) + * Added pvalloc, as recommended by H.J. Liu + * Added 64bit pointer support mainly from Wolfram Gloger + * Added anonymously donated WIN32 sbrk emulation + * Malloc, calloc, getpagesize: add optimizations from Raymond Nijssen + * malloc_extend_top: fix mask error that caused wastage after + foreign sbrks + * Add linux mremap support code from HJ Liu + + V2.6.2 Tue Dec 5 06:52:55 1995 Doug Lea (dl at gee) + * Integrated most documentation with the code. + * Add support for mmap, with help from + Wolfram Gloger (Gloger@lrz.uni-muenchen.de). + * Use last_remainder in more cases. + * Pack bins using idea from colin@nyx10.cs.du.edu + * Use ordered bins instead of best-fit threshhold + * Eliminate block-local decls to simplify tracing and debugging. + * Support another case of realloc via move into top + * Fix error occuring when initial sbrk_base not word-aligned. + * Rely on page size for units instead of SBRK_UNIT to + avoid surprises about sbrk alignment conventions. + * Add mallinfo, mallopt. Thanks to Raymond Nijssen + (raymond@es.ele.tue.nl) for the suggestion. + * Add `pad' argument to malloc_trim and top_pad mallopt parameter. + * More precautions for cases where other routines call sbrk, + courtesy of Wolfram Gloger (Gloger@lrz.uni-muenchen.de). + * Added macros etc., allowing use in linux libc from + H.J. Lu (hjl@gnu.ai.mit.edu) + * Inverted this history list + + V2.6.1 Sat Dec 2 14:10:57 1995 Doug Lea (dl at gee) + * Re-tuned and fixed to behave more nicely with V2.6.0 changes. + * Removed all preallocation code since under current scheme + the work required to undo bad preallocations exceeds + the work saved in good cases for most test programs. + * No longer use return list or unconsolidated bins since + no scheme using them consistently outperforms those that don't + given above changes. + * Use best fit for very large chunks to prevent some worst-cases. + * Added some support for debugging + + V2.6.0 Sat Nov 4 07:05:23 1995 Doug Lea (dl at gee) + * Removed footers when chunks are in use. Thanks to + Paul Wilson (wilson@cs.texas.edu) for the suggestion. + + V2.5.4 Wed Nov 1 07:54:51 1995 Doug Lea (dl at gee) + * Added malloc_trim, with help from Wolfram Gloger + (wmglo@Dent.MED.Uni-Muenchen.DE). + + V2.5.3 Tue Apr 26 10:16:01 1994 Doug Lea (dl at g) + + V2.5.2 Tue Apr 5 16:20:40 1994 Doug Lea (dl at g) + * realloc: try to expand in both directions + * malloc: swap order of clean-bin strategy; + * realloc: only conditionally expand backwards + * Try not to scavenge used bins + * Use bin counts as a guide to preallocation + * Occasionally bin return list chunks in first scan + * Add a few optimizations from colin@nyx10.cs.du.edu + + V2.5.1 Sat Aug 14 15:40:43 1993 Doug Lea (dl at g) + * faster bin computation & slightly different binning + * merged all consolidations to one part of malloc proper + (eliminating old malloc_find_space & malloc_clean_bin) + * Scan 2 returns chunks (not just 1) + * Propagate failure in realloc if malloc returns 0 + * Add stuff to allow compilation on non-ANSI compilers + from kpv@research.att.com + + V2.5 Sat Aug 7 07:41:59 1993 Doug Lea (dl at g.oswego.edu) + * removed potential for odd address access in prev_chunk + * removed dependency on getpagesize.h + * misc cosmetics and a bit more internal documentation + * anticosmetics: mangled names in macros to evade debugger strangeness + * tested on sparc, hp-700, dec-mips, rs6000 + with gcc & native cc (hp, dec only) allowing + Detlefs & Zorn comparison study (in SIGPLAN Notices.) + + Trial version Fri Aug 28 13:14:29 1992 Doug Lea (dl at g.oswego.edu) + * Based loosely on libg++-1.2X malloc. (It retains some of the overall + structure of old version, but most details differ.) + + */
\ No newline at end of file diff --git a/system/lib/libc.symbols b/system/lib/libc.symbols index b98a79e4..1342d938 100644 --- a/system/lib/libc.symbols +++ b/system/lib/libc.symbols @@ -1,3 +1,6 @@ +getopt +getopt_long +getopt_long_only malloc free calloc @@ -48,8 +51,25 @@ _ZNSt20bad_array_new_lengthC2Ev _ZNSt20bad_array_new_lengthD0Ev _ZNSt20bad_array_new_lengthD1Ev _ZNSt20bad_array_new_lengthD2Ev -memcpy -llvm.memcpy.i32 -llvm.memcpy.i64 -llvm.memcpy.p0i8.p0i8.i32 -llvm.memcpy.p0i8.p0i8.i64 +warn +warnx +err +errx +_warn +_warnx +_err +_errx +vwarn +vwarnx +verr +verrx +_vwarn +_vwarnx +_verr +_verrx +strtod +strtold +strtof +strtod_l +strtold_l +atof diff --git a/system/lib/libc/gen/err.c b/system/lib/libc/gen/err.c new file mode 100644 index 00000000..218a3bfc --- /dev/null +++ b/system/lib/libc/gen/err.c @@ -0,0 +1,49 @@ +/* $OpenBSD: err.c,v 1.11 2012/12/05 23:19:59 deraadt Exp $ */ +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <err.h> +#include <stdarg.h> + +#define __dead __attribute__((__noreturn__)) + +/* PRINTFLIKE2 */ +__dead void +_err(int eval, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + _verr(eval, fmt, ap); + va_end(ap); +} + +/* PRINTFLIKE2 */ +__dead void +err(int eval, const char *fmt, ...) __attribute__((weak, alias("_err"))); diff --git a/system/lib/libc/gen/errx.c b/system/lib/libc/gen/errx.c new file mode 100644 index 00000000..a16643c7 --- /dev/null +++ b/system/lib/libc/gen/errx.c @@ -0,0 +1,49 @@ +/* $OpenBSD: errx.c,v 1.10 2012/12/05 23:19:59 deraadt Exp $ */ +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <err.h> +#include <stdarg.h> + +#define __dead __attribute__((__noreturn__)) + +/* PRINTFLIKE2 */ +__dead void +_errx(int eval, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + _verrx(eval, fmt, ap); + va_end(ap); +} + +/* PRINTFLIKE2 */ +__dead void +errx(int eval, const char *fmt, ...) __attribute__((weak, alias("_errx"))); diff --git a/system/lib/libc/gen/verr.c b/system/lib/libc/gen/verr.c new file mode 100644 index 00000000..90f5870f --- /dev/null +++ b/system/lib/libc/gen/verr.c @@ -0,0 +1,58 @@ +/* $OpenBSD: verr.c,v 1.9 2012/12/05 23:20:00 deraadt Exp $ */ +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <err.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdarg.h> + +extern char *__progname; /* Program name, from crt0. */ + +#define __dead __attribute__((__noreturn__)) + +__dead void +_verr(int eval, const char *fmt, va_list ap) +{ + int sverrno; + + sverrno = errno; + (void)fprintf(stderr, "%s: ", __progname); + if (fmt != NULL) { + (void)vfprintf(stderr, fmt, ap); + (void)fprintf(stderr, ": "); + } + (void)fprintf(stderr, "%s\n", strerror(sverrno)); + exit(eval); +} + +__dead void +verr(int eval, const char *fmt, va_list ap) __attribute__((weak, alias("_verr"))); diff --git a/system/lib/libc/gen/verrx.c b/system/lib/libc/gen/verrx.c new file mode 100644 index 00000000..b712c65c --- /dev/null +++ b/system/lib/libc/gen/verrx.c @@ -0,0 +1,51 @@ +/* $OpenBSD: verrx.c,v 1.9 2012/12/05 23:20:00 deraadt Exp $ */ +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <err.h> +#include <stdio.h> +#include <stdlib.h> +#include <stdarg.h> + +extern char *__progname; /* Program name, from crt0. */ + +#define __dead __attribute__((__noreturn__)) + +__dead void +_verrx(int eval, const char *fmt, va_list ap) +{ + (void)fprintf(stderr, "%s: ", __progname); + if (fmt != NULL) + (void)vfprintf(stderr, fmt, ap); + (void)fprintf(stderr, "\n"); + exit(eval); +} + +__dead void +verrx(int eval, const char *fmt, va_list ap) __attribute__((weak, alias("_verrx"))); diff --git a/system/lib/libc/gen/vwarn.c b/system/lib/libc/gen/vwarn.c new file mode 100644 index 00000000..37acef35 --- /dev/null +++ b/system/lib/libc/gen/vwarn.c @@ -0,0 +1,55 @@ +/* $OpenBSD: vwarn.c,v 1.9 2012/12/05 23:20:00 deraadt Exp $ */ +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <err.h> +#include <errno.h> +#include <stdio.h> +#include <string.h> +#include <stdarg.h> + +extern char *__progname; /* Program name, from crt0. */ + +void +_vwarn(const char *fmt, va_list ap) +{ + int sverrno; + + sverrno = errno; + (void)fprintf(stderr, "%s: ", __progname); + if (fmt != NULL) { + (void)vfprintf(stderr, fmt, ap); + (void)fprintf(stderr, ": "); + } + (void)fprintf(stderr, "%s\n", strerror(sverrno)); +} + +void +vwarn(const char *fmt, va_list ap) __attribute__((weak, alias("_vwarn"))); + diff --git a/system/lib/libc/gen/vwarnx.c b/system/lib/libc/gen/vwarnx.c new file mode 100644 index 00000000..a81a5a0d --- /dev/null +++ b/system/lib/libc/gen/vwarnx.c @@ -0,0 +1,48 @@ +/* $OpenBSD: vwarnx.c,v 1.9 2012/12/05 23:20:00 deraadt Exp $ */ +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <err.h> +#include <stdio.h> +#include <stdarg.h> + +extern char *__progname; /* Program name, from crt0. */ + +void +_vwarnx(const char *fmt, va_list ap) +{ + (void)fprintf(stderr, "%s: ", __progname); + if (fmt != NULL) + (void)vfprintf(stderr, fmt, ap); + (void)fprintf(stderr, "\n"); +} + +void +vwarnx(const char *fmt, va_list ap) __attribute__((weak, alias("_vwarnx"))); + diff --git a/system/lib/libc/gen/warn.c b/system/lib/libc/gen/warn.c new file mode 100644 index 00000000..c0803ab9 --- /dev/null +++ b/system/lib/libc/gen/warn.c @@ -0,0 +1,49 @@ +/* $OpenBSD: warn.c,v 1.10 2012/12/05 23:20:00 deraadt Exp $ */ +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <err.h> +#include <stdarg.h> + +#define __dead __attribute__((__noreturn__)) + +/* PRINTFLIKE1 */ +void +_warn(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + _vwarn(fmt, ap); + va_end(ap); +} + +/* PRINTFLIKE1 */ +void +warn(const char *fmt, ...) __attribute__((weak, alias("warn"))); diff --git a/system/lib/libc/gen/warnx.c b/system/lib/libc/gen/warnx.c new file mode 100644 index 00000000..7909cf2c --- /dev/null +++ b/system/lib/libc/gen/warnx.c @@ -0,0 +1,49 @@ +/* $OpenBSD: warnx.c,v 1.9 2012/12/05 23:20:00 deraadt Exp $ */ +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <err.h> +#include <stdarg.h> + +#define __dead __attribute__((__noreturn__)) + +/* PRINTFLIKE1 */ +void +_warnx(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + _vwarnx(fmt, ap); + va_end(ap); +} + +/* PRINTFLIKE1 */ +void +warnx(const char *fmt, ...) __attribute__((weak, alias("_warnx"))); diff --git a/system/lib/libc/stdlib/getopt_long.c b/system/lib/libc/stdlib/getopt_long.c new file mode 100644 index 00000000..e149fe0a --- /dev/null +++ b/system/lib/libc/stdlib/getopt_long.c @@ -0,0 +1,511 @@ +/* $OpenBSD: getopt_long.c,v 1.25 2011/03/05 22:10:11 guenther Exp $ */ +/* $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $ */ + +/* + * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * Sponsored in part by the Defense Advanced Research Projects + * Agency (DARPA) and Air Force Research Laboratory, Air Force + * Materiel Command, USAF, under agreement number F39502-99-1-0512. + */ +/*- + * Copyright (c) 2000 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Dieter Baron and Thomas Klausner. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <err.h> +#include <errno.h> +#include <getopt.h> +#include <stdlib.h> +#include <string.h> + +int opterr = 1; /* if error message should be printed */ +int optind = 1; /* index into parent argv vector */ +int optopt = '?'; /* character checked for validity */ +int optreset; /* reset getopt */ +char *optarg; /* argument associated with option */ + +#define PRINT_ERROR ((opterr) && (*options != ':')) + +#define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */ +#define FLAG_ALLARGS 0x02 /* treat non-options as args to option "-1" */ +#define FLAG_LONGONLY 0x04 /* operate as getopt_long_only */ + +/* return values */ +#define BADCH (int)'?' +#define BADARG ((*options == ':') ? (int)':' : (int)'?') +#define INORDER (int)1 + +#define EMSG "" + +static int getopt_internal(int, char * const *, const char *, + const struct option *, int *, int); +static int parse_long_options(char * const *, const char *, + const struct option *, int *, int); +static int gcd(int, int); +static void permute_args(int, int, int, char * const *); + +static char *place = EMSG; /* option letter processing */ + +/* XXX: set optreset to 1 rather than these two */ +static int nonopt_start = -1; /* first non option argument (for permute) */ +static int nonopt_end = -1; /* first option after non options (for permute) */ + +/* Error messages */ +static const char recargchar[] = "option requires an argument -- %c"; +static const char recargstring[] = "option requires an argument -- %s"; +static const char ambig[] = "ambiguous option -- %.*s"; +static const char noarg[] = "option doesn't take an argument -- %.*s"; +static const char illoptchar[] = "unknown option -- %c"; +static const char illoptstring[] = "unknown option -- %s"; + +/* + * Compute the greatest common divisor of a and b. + */ +static int +gcd(int a, int b) +{ + int c; + + c = a % b; + while (c != 0) { + a = b; + b = c; + c = a % b; + } + + return (b); +} + +/* + * Exchange the block from nonopt_start to nonopt_end with the block + * from nonopt_end to opt_end (keeping the same order of arguments + * in each block). + */ +static void +permute_args(int panonopt_start, int panonopt_end, int opt_end, + char * const *nargv) +{ + int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos; + char *swap; + + /* + * compute lengths of blocks and number and size of cycles + */ + nnonopts = panonopt_end - panonopt_start; + nopts = opt_end - panonopt_end; + ncycle = gcd(nnonopts, nopts); + cyclelen = (opt_end - panonopt_start) / ncycle; + + for (i = 0; i < ncycle; i++) { + cstart = panonopt_end+i; + pos = cstart; + for (j = 0; j < cyclelen; j++) { + if (pos >= panonopt_end) + pos -= nnonopts; + else + pos += nopts; + swap = nargv[pos]; + /* LINTED const cast */ + ((char **) nargv)[pos] = nargv[cstart]; + /* LINTED const cast */ + ((char **)nargv)[cstart] = swap; + } + } +} + +/* + * parse_long_options -- + * Parse long options in argc/argv argument vector. + * Returns -1 if short_too is set and the option does not match long_options. + */ +static int +parse_long_options(char * const *nargv, const char *options, + const struct option *long_options, int *idx, int short_too) +{ + char *current_argv, *has_equal; + size_t current_argv_len; + int i, match; + + current_argv = place; + match = -1; + + optind++; + + if ((has_equal = strchr(current_argv, '=')) != NULL) { + /* argument found (--option=arg) */ + current_argv_len = has_equal - current_argv; + has_equal++; + } else + current_argv_len = strlen(current_argv); + + for (i = 0; long_options[i].name; i++) { + /* find matching long option */ + if (strncmp(current_argv, long_options[i].name, + current_argv_len)) + continue; + + if (strlen(long_options[i].name) == current_argv_len) { + /* exact match */ + match = i; + break; + } + /* + * If this is a known short option, don't allow + * a partial match of a single character. + */ + if (short_too && current_argv_len == 1) + continue; + + if (match == -1) /* partial match */ + match = i; + else { + /* ambiguous abbreviation */ + if (PRINT_ERROR) + warnx(ambig, (int)current_argv_len, + current_argv); + optopt = 0; + return (BADCH); + } + } + if (match != -1) { /* option found */ + if (long_options[match].has_arg == no_argument + && has_equal) { + if (PRINT_ERROR) + warnx(noarg, (int)current_argv_len, + current_argv); + /* + * XXX: GNU sets optopt to val regardless of flag + */ + if (long_options[match].flag == NULL) + optopt = long_options[match].val; + else + optopt = 0; + return (BADARG); + } + if (long_options[match].has_arg == required_argument || + long_options[match].has_arg == optional_argument) { + if (has_equal) + optarg = has_equal; + else if (long_options[match].has_arg == + required_argument) { + /* + * optional argument doesn't use next nargv + */ + optarg = nargv[optind++]; + } + } + if ((long_options[match].has_arg == required_argument) + && (optarg == NULL)) { + /* + * Missing argument; leading ':' indicates no error + * should be generated. + */ + if (PRINT_ERROR) + warnx(recargstring, + current_argv); + /* + * XXX: GNU sets optopt to val regardless of flag + */ + if (long_options[match].flag == NULL) + optopt = long_options[match].val; + else + optopt = 0; + --optind; + return (BADARG); + } + } else { /* unknown option */ + if (short_too) { + --optind; + return (-1); + } + if (PRINT_ERROR) + warnx(illoptstring, current_argv); + optopt = 0; + return (BADCH); + } + if (idx) + *idx = match; + if (long_options[match].flag) { + *long_options[match].flag = long_options[match].val; + return (0); + } else + return (long_options[match].val); +} + +/* + * getopt_internal -- + * Parse argc/argv argument vector. Called by user level routines. + */ +static int +getopt_internal(int nargc, char * const *nargv, const char *options, + const struct option *long_options, int *idx, int flags) +{ + char *oli; /* option letter list index */ + int optchar, short_too; + static int posixly_correct = -1; + + if (options == NULL) + return (-1); + + /* + * XXX Some GNU programs (like cvs) set optind to 0 instead of + * XXX using optreset. Work around this braindamage. + */ + if (optind == 0) + optind = optreset = 1; + + /* + * Disable GNU extensions if POSIXLY_CORRECT is set or options + * string begins with a '+'. + */ + if (posixly_correct == -1 || optreset) + posixly_correct = (getenv("POSIXLY_CORRECT") != NULL); + if (*options == '-') + flags |= FLAG_ALLARGS; + else if (posixly_correct || *options == '+') + flags &= ~FLAG_PERMUTE; + if (*options == '+' || *options == '-') + options++; + + optarg = NULL; + if (optreset) + nonopt_start = nonopt_end = -1; +start: + if (optreset || !*place) { /* update scanning pointer */ + optreset = 0; + if (optind >= nargc) { /* end of argument vector */ + place = EMSG; + if (nonopt_end != -1) { + /* do permutation, if we have to */ + permute_args(nonopt_start, nonopt_end, + optind, nargv); + optind -= nonopt_end - nonopt_start; + } + else if (nonopt_start != -1) { + /* + * If we skipped non-options, set optind + * to the first of them. + */ + optind = nonopt_start; + } + nonopt_start = nonopt_end = -1; + return (-1); + } + if (*(place = nargv[optind]) != '-' || + (place[1] == '\0' && strchr(options, '-') == NULL)) { + place = EMSG; /* found non-option */ + if (flags & FLAG_ALLARGS) { + /* + * GNU extension: + * return non-option as argument to option 1 + */ + optarg = nargv[optind++]; + return (INORDER); + } + if (!(flags & FLAG_PERMUTE)) { + /* + * If no permutation wanted, stop parsing + * at first non-option. + */ + return (-1); + } + /* do permutation */ + if (nonopt_start == -1) + nonopt_start = optind; + else if (nonopt_end != -1) { + permute_args(nonopt_start, nonopt_end, + optind, nargv); + nonopt_start = optind - + (nonopt_end - nonopt_start); + nonopt_end = -1; + } + optind++; + /* process next argument */ + goto start; + } + if (nonopt_start != -1 && nonopt_end == -1) + nonopt_end = optind; + + /* + * If we have "-" do nothing, if "--" we are done. + */ + if (place[1] != '\0' && *++place == '-' && place[1] == '\0') { + optind++; + place = EMSG; + /* + * We found an option (--), so if we skipped + * non-options, we have to permute. + */ + if (nonopt_end != -1) { + permute_args(nonopt_start, nonopt_end, + optind, nargv); + optind -= nonopt_end - nonopt_start; + } + nonopt_start = nonopt_end = -1; + return (-1); + } + } + + /* + * Check long options if: + * 1) we were passed some + * 2) the arg is not just "-" + * 3) either the arg starts with -- we are getopt_long_only() + */ + if (long_options != NULL && place != nargv[optind] && + (*place == '-' || (flags & FLAG_LONGONLY))) { + short_too = 0; + if (*place == '-') + place++; /* --foo long option */ + else if (*place != ':' && strchr(options, *place) != NULL) + short_too = 1; /* could be short option too */ + + optchar = parse_long_options(nargv, options, long_options, + idx, short_too); + if (optchar != -1) { + place = EMSG; + return (optchar); + } + } + + if ((optchar = (int)*place++) == (int)':' || + (optchar == (int)'-' && *place != '\0') || + (oli = strchr(options, optchar)) == NULL) { + /* + * If the user specified "-" and '-' isn't listed in + * options, return -1 (non-option) as per POSIX. + * Otherwise, it is an unknown option character (or ':'). + */ + if (optchar == (int)'-' && *place == '\0') + return (-1); + if (!*place) + ++optind; + if (PRINT_ERROR) + warnx(illoptchar, optchar); + optopt = optchar; + return (BADCH); + } + if (long_options != NULL && optchar == 'W' && oli[1] == ';') { + /* -W long-option */ + if (*place) /* no space */ + /* NOTHING */; + else if (++optind >= nargc) { /* no arg */ + place = EMSG; + if (PRINT_ERROR) + warnx(recargchar, optchar); + optopt = optchar; + return (BADARG); + } else /* white space */ + place = nargv[optind]; + optchar = parse_long_options(nargv, options, long_options, + idx, 0); + place = EMSG; + return (optchar); + } + if (*++oli != ':') { /* doesn't take argument */ + if (!*place) + ++optind; + } else { /* takes (optional) argument */ + optarg = NULL; + if (*place) /* no white space */ + optarg = place; + else if (oli[1] != ':') { /* arg not optional */ + if (++optind >= nargc) { /* no arg */ + place = EMSG; + if (PRINT_ERROR) + warnx(recargchar, optchar); + optopt = optchar; + return (BADARG); + } else + optarg = nargv[optind]; + } + place = EMSG; + ++optind; + } + /* dump back option letter */ + return (optchar); +} + +/* + * getopt -- + * Parse argc/argv argument vector. + * + * [eventually this will replace the BSD getopt] + */ +int +getopt(int nargc, char * const *nargv, const char *options) +{ + + /* + * We don't pass FLAG_PERMUTE to getopt_internal() since + * the BSD getopt(3) (unlike GNU) has never done this. + * + * Furthermore, since many privileged programs call getopt() + * before dropping privileges it makes sense to keep things + * as simple (and bug-free) as possible. + */ + return (getopt_internal(nargc, nargv, options, NULL, NULL, 0)); +} + +/* + * getopt_long -- + * Parse argc/argv argument vector. + */ +int +getopt_long(int nargc, char * const *nargv, const char *options, + const struct option *long_options, int *idx) +{ + + return (getopt_internal(nargc, nargv, options, long_options, idx, + FLAG_PERMUTE)); +} + +/* + * getopt_long_only -- + * Parse argc/argv argument vector. + */ +int +getopt_long_only(int nargc, char * const *nargv, const char *options, + const struct option *long_options, int *idx) +{ + + return (getopt_internal(nargc, nargv, options, long_options, idx, + FLAG_PERMUTE|FLAG_LONGONLY)); +} diff --git a/system/lib/libc/stdlib/strtod.c b/system/lib/libc/stdlib/strtod.c new file mode 100644 index 00000000..53191337 --- /dev/null +++ b/system/lib/libc/stdlib/strtod.c @@ -0,0 +1,305 @@ +/* + * strtod.c -- + * + * Source code for the "strtod" library procedure. + * + * Copyright (c) 1988-1993 The Regents of the University of California. + * Copyright (c) 1994 Sun Microsystems, Inc. + * + * Permission to use, copy, modify, and distribute this + * software and its documentation for any purpose and without + * fee is hereby granted, provided that the above copyright + * notice appear in all copies. The University of California + * makes no representations about the suitability of this + * software for any purpose. It is provided "as is" without + * express or implied warranty. + * + * RCS: @(#) $Id$ + * + * Taken from http://svn.ruby-lang.org/repos/ruby/branches/ruby_1_8/missing/strtod.c + */ + +#include <stdlib.h> +#include <ctype.h> +#include <errno.h> +#include <locale.h> +extern int errno; + +#ifndef __STDC__ +# ifdef __GNUC__ +# define const __const__ +# else +# define const +# endif +#endif + +#ifndef TRUE +#define TRUE 1 +#define FALSE 0 +#endif +#ifndef NULL +#define NULL 0 +#endif + +static int maxExponent = 511; /* Largest possible base 10 exponent. Any + * exponent larger than this will already + * produce underflow or overflow, so there's + * no need to worry about additional digits. + */ +static double powersOf10[] = { /* Table giving binary powers of 10. Entry */ + 10., /* is 10^2^i. Used to convert decimal */ + 100., /* exponents into floating-point numbers. */ + 1.0e4, + 1.0e8, + 1.0e16, + 1.0e32, + 1.0e64, + 1.0e128, + 1.0e256 +}; + +/* + *---------------------------------------------------------------------- + * + * strtod -- + * + * This procedure converts a floating-point number from an ASCII + * decimal representation to internal double-precision format. + * + * Results: + * The return value is the double-precision floating-point + * representation of the characters in string. If endPtr isn't + * NULL, then *endPtr is filled in with the address of the + * next character after the last one that was part of the + * floating-point number. + * + * Side effects: + * None. + * + *---------------------------------------------------------------------- + */ + +double +strtod(string, endPtr) + const char *string; /* A decimal ASCII floating-point number, + * optionally preceded by white space. + * Must have form "-I.FE-X", where I is the + * integer part of the mantissa, F is the + * fractional part of the mantissa, and X + * is the exponent. Either of the signs + * may be "+", "-", or omitted. Either I + * or F may be omitted, or both. The decimal + * point isn't necessary unless F is present. + * The "E" may actually be an "e". E and X + * may both be omitted (but not just one). + */ + char **endPtr; /* If non-NULL, store terminating character's + * address here. */ +{ + int sign, expSign = FALSE; + double fraction, dblExp, *d; + register const char *p; + register int c; + int exp = 0; /* Exponent read from "EX" field. */ + int fracExp = 0; /* Exponent that derives from the fractional + * part. Under normal circumstatnces, it is + * the negative of the number of digits in F. + * However, if I is very long, the last digits + * of I get dropped (otherwise a long I with a + * large negative exponent could cause an + * unnecessary overflow on I alone). In this + * case, fracExp is incremented one for each + * dropped digit. */ + int mantSize; /* Number of digits in mantissa. */ + int decPt; /* Number of mantissa digits BEFORE decimal + * point. */ + const char *pExp; /* Temporarily holds location of exponent + * in string. */ + + /* + * Strip off leading blanks and check for a sign. + */ + + p = string; + while (isspace(*p)) { + p += 1; + } + if (*p == '-') { + sign = TRUE; + p += 1; + } else { + if (*p == '+') { + p += 1; + } + sign = FALSE; + } + + /* + * Count the number of digits in the mantissa (including the decimal + * point), and also locate the decimal point. + */ + + decPt = -1; + for (mantSize = 0; ; mantSize += 1) + { + c = *p; + if (!isdigit(c)) { + if ((c != '.') || (decPt >= 0)) { + break; + } + decPt = mantSize; + } + p += 1; + } + + /* + * Now suck up the digits in the mantissa. Use two integers to + * collect 9 digits each (this is faster than using floating-point). + * If the mantissa has more than 18 digits, ignore the extras, since + * they can't affect the value anyway. + */ + + pExp = p; + p -= mantSize; + if (decPt < 0) { + decPt = mantSize; + } else { + mantSize -= 1; /* One of the digits was the point. */ + } + if (mantSize > 18) { + fracExp = decPt - 18; + mantSize = 18; + } else { + fracExp = decPt - mantSize; + } + if (mantSize == 0) { + fraction = 0.0; + p = string; + goto done; + } else { + int frac1, frac2; + frac1 = 0; + for ( ; mantSize > 9; mantSize -= 1) + { + c = *p; + p += 1; + if (c == '.') { + c = *p; + p += 1; + } + frac1 = 10*frac1 + (c - '0'); + } + frac2 = 0; + for (; mantSize > 0; mantSize -= 1) + { + c = *p; + p += 1; + if (c == '.') { + c = *p; + p += 1; + } + frac2 = 10*frac2 + (c - '0'); + } + fraction = (1.0e9 * frac1) + frac2; + } + + /* + * Skim off the exponent. + */ + + p = pExp; + if ((*p == 'E') || (*p == 'e')) { + p += 1; + if (*p == '-') { + expSign = TRUE; + p += 1; + } else { + if (*p == '+') { + p += 1; + } + expSign = FALSE; + } + while (isdigit(*p)) { + exp = exp * 10 + (*p - '0'); + p += 1; + } + } + if (expSign) { + exp = fracExp - exp; + } else { + exp = fracExp + exp; + } + + /* + * Generate a floating-point number that represents the exponent. + * Do this by processing the exponent one bit at a time to combine + * many powers of 2 of 10. Then combine the exponent with the + * fraction. + */ + + if (exp < 0) { + expSign = TRUE; + exp = -exp; + } else { + expSign = FALSE; + } + if (exp > maxExponent) { + exp = maxExponent; + errno = ERANGE; + } + dblExp = 1.0; + for (d = powersOf10; exp != 0; exp >>= 1, d += 1) { + if (exp & 01) { + dblExp *= *d; + } + } + if (expSign) { + fraction /= dblExp; + } else { + fraction *= dblExp; + } + +done: + if (endPtr != NULL) { + *endPtr = (char *) p; + } + + if (sign) { + return -fraction; + } + return fraction; +} + +/* + * Implementations added for emscripten. + */ +// XXX add real support for long double +long double +strtold(const char* nptr, char **endptr) +{ + return (long double) strtod(nptr, endptr); +} + +// use stdtod to handle strtof +float +strtof(const char* nptr, char **endptr) +{ + return (float) strtof(nptr, endptr); +} + +// XXX no locale support yet +double +strtod_l(const char* nptr, char **endptr, locale_t loc) +{ + return strtod(nptr, endptr); +} +long double +strtold_l(const char* nptr, char **endptr, locale_t loc) +{ + return strtold(nptr, endptr); +} + +double atof(const char* str) +{ + return strtod(str, (char **) NULL); +} diff --git a/system/lib/libcxx/symbols b/system/lib/libcxx/symbols index 23d4a7a4..0d412de7 100644 --- a/system/lib/libcxx/symbols +++ b/system/lib/libcxx/symbols @@ -767,7 +767,6 @@ W _ZNKSt3__19money_putIwNS_19ostreambuf_iteratorIwNS_11char_traitsIwEEEEE3putES4_bRNS_8ios_baseEwe W _ZNKSt3__19money_putIwNS_19ostreambuf_iteratorIwNS_11char_traitsIwEEEEE6do_putES4_bRNS_8ios_baseEwRKNS_12basic_stringIwS3_NS_9allocatorIwEEEE W _ZNKSt3__19money_putIwNS_19ostreambuf_iteratorIwNS_11char_traitsIwEEEEE6do_putES4_bRNS_8ios_baseEwe - T _ZNKSt8bad_cast4whatEv T _ZNKSt9exception4whatEv T _ZNSt10bad_typeidC1Ev T _ZNSt10bad_typeidC2Ev @@ -2561,11 +2560,6 @@ d _ZNSt3__1L7__wcoutE d _ZNSt3__1L8__rs_mutE W _ZNSt3__1plIcNS_11char_traitsIcEENS_9allocatorIcEEEENS_12basic_stringIT_T0_T1_EEPKS6_RKS9_ - T _ZNSt8bad_castC1Ev - T _ZNSt8bad_castC2Ev - T _ZNSt8bad_castD0Ev - T _ZNSt8bad_castD1Ev - T _ZNSt8bad_castD2Ev T _ZNSt9exceptionD0Ev T _ZNSt9exceptionD1Ev T _ZNSt9exceptionD2Ev @@ -2719,7 +2713,6 @@ D _ZTISt15underflow_error D _ZTISt16invalid_argument D _ZTISt16nested_exception - D _ZTISt8bad_cast D _ZTISt9exception C _ZTSNSt3__110__stdinbufIcEE C _ZTSNSt3__110__stdinbufIwEE @@ -2855,7 +2848,6 @@ D _ZTSSt15underflow_error D _ZTSSt16invalid_argument D _ZTSSt16nested_exception - D _ZTSSt8bad_cast D _ZTSSt9exception D _ZTTNSt3__110istrstreamE D _ZTTNSt3__110ostrstreamE @@ -2980,7 +2972,6 @@ D _ZTVSt15underflow_error D _ZTVSt16invalid_argument D _ZTVSt16nested_exception - D _ZTVSt8bad_cast D _ZTVSt9exception W _ZThn8_NKSt3__115time_get_bynameIcNS_19istreambuf_iteratorIcNS_11char_traitsIcEEEEE3__XEv W _ZThn8_NKSt3__115time_get_bynameIcNS_19istreambuf_iteratorIcNS_11char_traitsIcEEEEE3__cEv diff --git a/system/lib/libcxxabi/symbols b/system/lib/libcxxabi/symbols index 8ef205ba..b63f2f82 100644 --- a/system/lib/libcxxabi/symbols +++ b/system/lib/libcxxabi/symbols @@ -10,3 +10,10 @@ D _ZTVN10__cxxabiv121__vmi_class_type_infoE D _ZTVN10__cxxabiv123__fundamental_type_infoE D _ZTVN10__cxxabiv129__pointer_to_member_type_infoE + D _ZTSSt9type_info + T _ZNKSt8bad_cast4whatEv + T _ZNSt8bad_castC1Ev + T _ZNSt8bad_castC2Ev + D _ZTISt8bad_cast + D _ZTSSt8bad_cast + D _ZTVSt8bad_cast diff --git a/tests/aniso.c b/tests/aniso.c index e673e228..e02c20ac 100644 --- a/tests/aniso.c +++ b/tests/aniso.c @@ -145,6 +145,11 @@ int main(int argc, char *argv[]) glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso); } + { + assert(!glGetError()); + glBindFramebuffer(GL_RENDERBUFFER, 0); + assert(glGetError()); + } // Prepare and Render diff --git a/tests/aniso.png b/tests/aniso.png Binary files differindex 5f5812d2..2bcb2f5f 100644 --- a/tests/aniso.png +++ b/tests/aniso.png diff --git a/tests/bullet/output3.txt b/tests/bullet/output3.txt new file mode 100644 index 00000000..78f27f9f --- /dev/null +++ b/tests/bullet/output3.txt @@ -0,0 +1,270 @@ +world pos = 2.00,10.00,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,9.99,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,9.98,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,9.97,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,9.96,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,9.94,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,9.92,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,9.90,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,9.88,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,9.85,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,9.82,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,9.78,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,9.75,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,9.71,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,9.67,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,9.62,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,9.57,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,9.52,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,9.47,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,9.42,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,9.36,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,9.30,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,9.23,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,9.17,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,9.10,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,9.02,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,8.95,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,8.87,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,8.79,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,8.71,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,8.62,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,8.53,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,8.44,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,8.35,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,8.25,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,8.15,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,8.05,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,7.94,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,7.83,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,7.72,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,7.61,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,7.49,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,7.37,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,7.25,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,7.13,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,7.00,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,6.87,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,6.73,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,6.60,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,6.46,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,6.32,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,6.17,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,6.03,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,5.88,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,5.72,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,5.57,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,5.41,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,5.25,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,5.08,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,4.92,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,4.75,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,4.58,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,4.40,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,4.22,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,4.04,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,3.86,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,3.67,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,3.48,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,3.29,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,3.10,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,2.90,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,2.70,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,2.50,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,2.29,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,2.08,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,1.87,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,1.66,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,1.44,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,1.22,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,1.00,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,0.77,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,0.55,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,0.32,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,0.08,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-0.15,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-0.39,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-0.63,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-0.88,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-1.13,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-1.38,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-1.63,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-1.88,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-2.14,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-2.40,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-2.67,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-2.93,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-3.20,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-3.48,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-3.75,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-4.03,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-4.31,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-4.59,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-4.88,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-5.17,0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-5.13,-0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-5.10,-0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-5.08,-0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-5.05,-0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-5.03,-0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-5.01,-0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-4.99,-0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-4.98,-0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-4.97,-0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-4.96,-0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-4.95,-0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-4.95,-0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-4.95,-0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-4.95,-0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-4.96,-0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-4.97,-0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-4.98,-0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-4.99,-0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-5.00,-0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-5.00,-0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-5.00,-0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-5.00,-0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-5.00,-0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-5.00,-0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-5.00,-0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-5.00,-0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-5.00,-0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-5.00,-0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-5.00,-0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-5.00,-0.00 +world pos = 0.00,-56.00,0.00 +world pos = 2.00,-5.00,-0.00 +world pos = 0.00,-56.00,0.00 diff --git a/tests/cases/atomicrmw.ll b/tests/cases/atomicrmw.ll index 2f5a4224..fe479dce 100644 --- a/tests/cases/atomicrmw.ll +++ b/tests/cases/atomicrmw.ll @@ -13,6 +13,7 @@ entry: %1 = atomicrmw add i32* %t, i32 3 seq_cst, ; [#uses=0 type=i32] [debug line = 21:12] %2 = load i32* %t %call = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([15 x i8]* @.str, i32 0, i32 0), i32 %0, i32 %2) ; [#uses=0 type=i32] + %3 = atomicrmw volatile add i32* %t, i32 3 seq_cst, ; [#uses=0 type=i32] [debug line = 21:12] ret i32 1 } diff --git a/tests/cases/breakinthemiddle2.ll b/tests/cases/breakinthemiddle2.ll new file mode 100644 index 00000000..318b49dc --- /dev/null +++ b/tests/cases/breakinthemiddle2.ll @@ -0,0 +1,35 @@ +@.str = private constant [15 x i8] c"hello, world!\0A\00", align 1 ; [#uses=1] + +define linkonce_odr i32 @main() align 2 { + %333 = call i32 @printf(i8* getelementptr inbounds ([15 x i8]* @.str, i32 0, i32 0)) ; [#uses=0] + %199 = trunc i8 1 to i1 ; [#uses=1] + br i1 %199, label %label555, label %label569 + +label555: ; preds = %0 + br label %label569 ; branch should ignore all code after it in the block + ; No predecessors! + %a472 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) + cleanup + %a473 = extractvalue { i8*, i32 } %a472, 0 + %a474 = extractvalue { i8*, i32 } %a472, 1 + br label %label569 + +label569: ; preds = %0 + br i1 %199, label %label990, label %label999 + +label990: + ret i32 0 ; ret should ignore all code after it in the block + ; No predecessors! + %a472 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) + cleanup + %a473 = extractvalue { i8*, i32 } %a472, 0 + %a474 = extractvalue { i8*, i32 } %a472, 1 + br label %label569 + +label999: ; preds = %555 + ret i32 0 +} + +declare i32 @printf(i8*) +declare i32 @__gxx_personality_v0(...) + diff --git a/tests/cases/callalias.ll b/tests/cases/callalias.ll new file mode 100644 index 00000000..9bc1ffd0 --- /dev/null +++ b/tests/cases/callalias.ll @@ -0,0 +1,21 @@ +; ModuleID = 'tests/hello_world.bc' +target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32-S128" +target triple = "i386-pc-linux-gnu" + +@.str = private unnamed_addr constant [15 x i8] c"hello, world!\0A\00", align 1 ; [#uses=1 type=[15 x i8]*] + +@othername = alias internal void ()* @doit + +define internal void @doit() unnamed_addr nounwind align 2 { + %call = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([15 x i8]* @.str, i32 0, i32 0)) ; [#uses=0 type=i32] + ret void +} + +define i32 @main() { +entry: + tail call void ()* @othername() nounwind + ret i32 1 +} + +declare i32 @printf(i8*, ...) + diff --git a/tests/cases/callalias2.ll b/tests/cases/callalias2.ll new file mode 100644 index 00000000..abdbe4e6 --- /dev/null +++ b/tests/cases/callalias2.ll @@ -0,0 +1,22 @@ +; ModuleID = 'tests/hello_world.bc' +target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32-S128" +target triple = "i386-pc-linux-gnu" + +@.str = private unnamed_addr constant [15 x i8] c"hello, world!\0A\00", align 1 ; [#uses=1 type=[15 x i8]*] + +@othername = alias internal void ()* @doit +@othername2 = alias internal void ()* @othername + +define internal void @doit() unnamed_addr nounwind align 2 { + %call = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([15 x i8]* @.str, i32 0, i32 0)) ; [#uses=0 type=i32] + ret void +} + +define i32 @main() { +entry: + tail call void ()* @othername2() nounwind + ret i32 1 +} + +declare i32 @printf(i8*, ...) + diff --git a/tests/cases/cmpxchg_volatile.ll b/tests/cases/cmpxchg_volatile.ll new file mode 100644 index 00000000..019fd833 --- /dev/null +++ b/tests/cases/cmpxchg_volatile.ll @@ -0,0 +1,548 @@ +; ModuleID = 'ta2.bc' +target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32-S128" +target triple = "i386-pc-linux-gnu" + +%"struct.std::__1::__atomic_base.0" = type { i8 } +%"struct.std::__1::__atomic_base" = type { %"struct.std::__1::__atomic_base.0" } +%"struct.std::__1::atomic" = type { %"struct.std::__1::__atomic_base" } + +@.str = private unnamed_addr constant [8 x i8] c"ta2.cpp\00", align 1 +@__PRETTY_FUNCTION__._Z7do_testIVNSt3__16atomicIcEEcEvv = private unnamed_addr constant [63 x i8] c"void do_test() [A = volatile std::__1::atomic<char>, T = char]\00", align 1 +@.str1 = private unnamed_addr constant [43 x i8] c"obj.compare_exchange_weak(x, T(2)) == true\00", align 1 +@.str2 = private unnamed_addr constant [12 x i8] c"obj == T(2)\00", align 1 +@.str3 = private unnamed_addr constant [10 x i8] c"x == T(3)\00", align 1 +@.str4 = private unnamed_addr constant [44 x i8] c"obj.compare_exchange_weak(x, T(1)) == false\00", align 1 +@.str5 = private unnamed_addr constant [10 x i8] c"x == T(2)\00", align 1 +@.str6 = private unnamed_addr constant [45 x i8] c"obj.compare_exchange_strong(x, T(1)) == true\00", align 1 +@.str7 = private unnamed_addr constant [12 x i8] c"obj == T(1)\00", align 1 +@.str8 = private unnamed_addr constant [15 x i8] c"hello, world!\0A\00", align 1 ; [#uses=1 type=[15 x i8]*] + +define i32 @main() ssp { +entry: + %call = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([15 x i8]* @.str8, i32 0, i32 0)) ; [#uses=0 type=i32] + call void @_Z4testIVNSt3__16atomicIcEEcEvv() + ret i32 0 +} + +define linkonce_odr void @_Z4testIVNSt3__16atomicIcEEcEvv() ssp { +entry: + call void @_Z7do_testIVNSt3__16atomicIcEEcEvv() + call void @_Z7do_testIVNSt3__16atomicIcEEcEvv() + ret void +} + +define linkonce_odr void @_Z7do_testIVNSt3__16atomicIcEEcEvv() ssp { +entry: + %this.addr.i.i110 = alloca %"struct.std::__1::__atomic_base.0"*, align 4 + %__m.addr.i.i111 = alloca i32, align 4 + %.atomicdst.i.i112 = alloca i8, align 1 + %this.addr.i113 = alloca %"struct.std::__1::__atomic_base.0"*, align 4 + %this.addr.i90 = alloca %"struct.std::__1::__atomic_base.0"*, align 4 + %__e.addr.i91 = alloca i8*, align 4 + %__d.addr.i92 = alloca i8, align 1 + %__m.addr.i93 = alloca i32, align 4 + %.atomictmp.i94 = alloca i8, align 1 + %.atomicdst.i95 = alloca i8, align 1 + %this.addr.i.i79 = alloca %"struct.std::__1::__atomic_base.0"*, align 4 + %__m.addr.i.i80 = alloca i32, align 4 + %.atomicdst.i.i81 = alloca i8, align 1 + %this.addr.i82 = alloca %"struct.std::__1::__atomic_base.0"*, align 4 + %this.addr.i60 = alloca %"struct.std::__1::__atomic_base.0"*, align 4 + %__e.addr.i61 = alloca i8*, align 4 + %__d.addr.i62 = alloca i8, align 1 + %__m.addr.i63 = alloca i32, align 4 + %.atomictmp.i64 = alloca i8, align 1 + %.atomicdst.i65 = alloca i8, align 1 + %this.addr.i.i49 = alloca %"struct.std::__1::__atomic_base.0"*, align 4 + %__m.addr.i.i50 = alloca i32, align 4 + %.atomicdst.i.i51 = alloca i8, align 1 + %this.addr.i52 = alloca %"struct.std::__1::__atomic_base.0"*, align 4 + %this.addr.i46 = alloca %"struct.std::__1::__atomic_base.0"*, align 4 + %__e.addr.i = alloca i8*, align 4 + %__d.addr.i47 = alloca i8, align 1 + %__m.addr.i = alloca i32, align 4 + %.atomictmp.i = alloca i8, align 1 + %.atomicdst.i = alloca i8, align 1 + %this.addr.i.i42 = alloca %"struct.std::__1::__atomic_base.0"*, align 4 + %__m.addr.i.i = alloca i32, align 4 + %.atomicdst.i.i = alloca i8, align 1 + %this.addr.i43 = alloca %"struct.std::__1::__atomic_base.0"*, align 4 + %this.addr.i.i.i.i = alloca %"struct.std::__1::__atomic_base.0"*, align 4 + %__d.addr.i.i.i.i = alloca i8, align 1 + %this.addr.i.i.i = alloca %"struct.std::__1::__atomic_base"*, align 4 + %__d.addr.i.i.i = alloca i8, align 1 + %this.addr.i.i = alloca %"struct.std::__1::atomic"*, align 4 + %__d.addr.i.i = alloca i8, align 1 + %this.addr.i = alloca %"struct.std::__1::atomic"*, align 4 + %__d.addr.i = alloca i8, align 1 + %obj = alloca %"struct.std::__1::atomic", align 1 + %x = alloca i8, align 1 + store %"struct.std::__1::atomic"* %obj, %"struct.std::__1::atomic"** %this.addr.i, align 4 + store i8 0, i8* %__d.addr.i, align 1 + %this1.i = load %"struct.std::__1::atomic"** %this.addr.i + %0 = load i8* %__d.addr.i, align 1 + store %"struct.std::__1::atomic"* %this1.i, %"struct.std::__1::atomic"** %this.addr.i.i, align 4 + store i8 %0, i8* %__d.addr.i.i, align 1 + %this1.i.i = load %"struct.std::__1::atomic"** %this.addr.i.i + %1 = bitcast %"struct.std::__1::atomic"* %this1.i.i to %"struct.std::__1::__atomic_base"* + %2 = load i8* %__d.addr.i.i, align 1 + store %"struct.std::__1::__atomic_base"* %1, %"struct.std::__1::__atomic_base"** %this.addr.i.i.i, align 4 + store i8 %2, i8* %__d.addr.i.i.i, align 1 + %this1.i.i.i = load %"struct.std::__1::__atomic_base"** %this.addr.i.i.i + %3 = bitcast %"struct.std::__1::__atomic_base"* %this1.i.i.i to %"struct.std::__1::__atomic_base.0"* + %4 = load i8* %__d.addr.i.i.i, align 1 + store %"struct.std::__1::__atomic_base.0"* %3, %"struct.std::__1::__atomic_base.0"** %this.addr.i.i.i.i, align 4 + store i8 %4, i8* %__d.addr.i.i.i.i, align 1 + %this1.i.i.i.i = load %"struct.std::__1::__atomic_base.0"** %this.addr.i.i.i.i + %__a_.i.i.i.i = getelementptr inbounds %"struct.std::__1::__atomic_base.0"* %this1.i.i.i.i, i32 0, i32 0 + %5 = load i8* %__d.addr.i.i.i.i, align 1 + store i8 %5, i8* %__a_.i.i.i.i, align 1 + %6 = bitcast %"struct.std::__1::atomic"* %obj to %"struct.std::__1::__atomic_base.0"* + store %"struct.std::__1::__atomic_base.0"* %6, %"struct.std::__1::__atomic_base.0"** %this.addr.i113, align 4 + %this1.i114 = load %"struct.std::__1::__atomic_base.0"** %this.addr.i113 + store %"struct.std::__1::__atomic_base.0"* %this1.i114, %"struct.std::__1::__atomic_base.0"** %this.addr.i.i110, align 4 + store i32 5, i32* %__m.addr.i.i111, align 4 + %this1.i.i115 = load %"struct.std::__1::__atomic_base.0"** %this.addr.i.i110 + %__a_.i.i116 = getelementptr inbounds %"struct.std::__1::__atomic_base.0"* %this1.i.i115, i32 0, i32 0 + %7 = load i32* %__m.addr.i.i111, align 4 + switch i32 %7, label %monotonic.i.i117 [ + i32 1, label %acquire.i.i118 + i32 2, label %acquire.i.i118 + i32 5, label %seqcst.i.i119 + ] + +monotonic.i.i117: ; preds = %entry + %8 = load atomic volatile i8* %__a_.i.i116 monotonic, align 1 + store i8 %8, i8* %.atomicdst.i.i112, align 1 + br label %_ZNVKSt3__113__atomic_baseIcLb0EEcvcEv.exit120 + +acquire.i.i118: ; preds = %entry, %entry + %9 = load atomic volatile i8* %__a_.i.i116 acquire, align 1 + store i8 %9, i8* %.atomicdst.i.i112, align 1 + br label %_ZNVKSt3__113__atomic_baseIcLb0EEcvcEv.exit120 + +seqcst.i.i119: ; preds = %entry + %10 = load atomic volatile i8* %__a_.i.i116 seq_cst, align 1 + store i8 %10, i8* %.atomicdst.i.i112, align 1 + br label %_ZNVKSt3__113__atomic_baseIcLb0EEcvcEv.exit120 + +_ZNVKSt3__113__atomic_baseIcLb0EEcvcEv.exit120: ; preds = %seqcst.i.i119, %acquire.i.i118, %monotonic.i.i117 + %11 = load i8* %.atomicdst.i.i112 + store i8 %11, i8* %x, align 1 + %12 = bitcast %"struct.std::__1::atomic"* %obj to %"struct.std::__1::__atomic_base.0"* + store %"struct.std::__1::__atomic_base.0"* %12, %"struct.std::__1::__atomic_base.0"** %this.addr.i90, align 4 + store i8* %x, i8** %__e.addr.i91, align 4 + store i8 2, i8* %__d.addr.i92, align 1 + store i32 5, i32* %__m.addr.i93, align 4 + %this1.i96 = load %"struct.std::__1::__atomic_base.0"** %this.addr.i90 + %__a_.i97 = getelementptr inbounds %"struct.std::__1::__atomic_base.0"* %this1.i96, i32 0, i32 0 + %13 = load i32* %__m.addr.i93, align 4 + %14 = load i8** %__e.addr.i91, align 4 + %15 = load i8* %__d.addr.i92, align 1 + store i8 %15, i8* %.atomictmp.i94 + %16 = load i32* %__m.addr.i93, align 4 + switch i32 %13, label %monotonic.i99 [ + i32 1, label %acquire.i101 + i32 2, label %acquire.i101 + i32 3, label %release.i103 + i32 4, label %acqrel.i105 + i32 5, label %seqcst.i107 + ] + +monotonic.i99: ; preds = %_ZNVKSt3__113__atomic_baseIcLb0EEcvcEv.exit120 + %17 = load i8* %14, align 1 + %18 = load i8* %.atomictmp.i94, align 1 + %19 = cmpxchg volatile i8* %__a_.i97, i8 %17, i8 %18 monotonic + store i8 %19, i8* %14, align 1 + %20 = icmp eq i8 %19, %17 + %frombool.i98 = zext i1 %20 to i8 + store i8 %frombool.i98, i8* %.atomicdst.i95 + br label %_ZNVSt3__113__atomic_baseIcLb0EE21compare_exchange_weakERccNS_12memory_orderE.exit109 + +acquire.i101: ; preds = %_ZNVKSt3__113__atomic_baseIcLb0EEcvcEv.exit120, %_ZNVKSt3__113__atomic_baseIcLb0EEcvcEv.exit120 + %21 = load i8* %14, align 1 + %22 = load i8* %.atomictmp.i94, align 1 + %23 = cmpxchg volatile i8* %__a_.i97, i8 %21, i8 %22 acquire + store i8 %23, i8* %14, align 1 + %24 = icmp eq i8 %23, %21 + %frombool2.i100 = zext i1 %24 to i8 + store i8 %frombool2.i100, i8* %.atomicdst.i95 + br label %_ZNVSt3__113__atomic_baseIcLb0EE21compare_exchange_weakERccNS_12memory_orderE.exit109 + +release.i103: ; preds = %_ZNVKSt3__113__atomic_baseIcLb0EEcvcEv.exit120 + %25 = load i8* %14, align 1 + %26 = load i8* %.atomictmp.i94, align 1 + %27 = cmpxchg volatile i8* %__a_.i97, i8 %25, i8 %26 release + store i8 %27, i8* %14, align 1 + %28 = icmp eq i8 %27, %25 + %frombool3.i102 = zext i1 %28 to i8 + store i8 %frombool3.i102, i8* %.atomicdst.i95 + br label %_ZNVSt3__113__atomic_baseIcLb0EE21compare_exchange_weakERccNS_12memory_orderE.exit109 + +acqrel.i105: ; preds = %_ZNVKSt3__113__atomic_baseIcLb0EEcvcEv.exit120 + %29 = load i8* %14, align 1 + %30 = load i8* %.atomictmp.i94, align 1 + %31 = cmpxchg volatile i8* %__a_.i97, i8 %29, i8 %30 acq_rel + store i8 %31, i8* %14, align 1 + %32 = icmp eq i8 %31, %29 + %frombool4.i104 = zext i1 %32 to i8 + store i8 %frombool4.i104, i8* %.atomicdst.i95 + br label %_ZNVSt3__113__atomic_baseIcLb0EE21compare_exchange_weakERccNS_12memory_orderE.exit109 + +seqcst.i107: ; preds = %_ZNVKSt3__113__atomic_baseIcLb0EEcvcEv.exit120 + %33 = load i8* %14, align 1 + %34 = load i8* %.atomictmp.i94, align 1 + %35 = cmpxchg volatile i8* %__a_.i97, i8 %33, i8 %34 seq_cst + store i8 %35, i8* %14, align 1 + %36 = icmp eq i8 %35, %33 + %frombool5.i106 = zext i1 %36 to i8 + store i8 %frombool5.i106, i8* %.atomicdst.i95 + br label %_ZNVSt3__113__atomic_baseIcLb0EE21compare_exchange_weakERccNS_12memory_orderE.exit109 + +_ZNVSt3__113__atomic_baseIcLb0EE21compare_exchange_weakERccNS_12memory_orderE.exit109: ; preds = %seqcst.i107, %acqrel.i105, %release.i103, %acquire.i101, %monotonic.i99 + %37 = load i8* %.atomicdst.i95 + %tobool.i108 = trunc i8 %37 to i1 + %conv = zext i1 %tobool.i108 to i32 + %cmp = icmp eq i32 %conv, 1 + br i1 %cmp, label %cond.true, label %cond.false + +cond.true: ; preds = %_ZNVSt3__113__atomic_baseIcLb0EE21compare_exchange_weakERccNS_12memory_orderE.exit109 + br label %cond.end + +cond.false: ; preds = %_ZNVSt3__113__atomic_baseIcLb0EE21compare_exchange_weakERccNS_12memory_orderE.exit109 + call void @__assert_func(i8* getelementptr inbounds ([8 x i8]* @.str, i32 0, i32 0), i32 21, i8* getelementptr inbounds ([63 x i8]* @__PRETTY_FUNCTION__._Z7do_testIVNSt3__16atomicIcEEcEvv, i32 0, i32 0), i8* getelementptr inbounds ([43 x i8]* @.str1, i32 0, i32 0)) + br label %cond.end + +cond.end: ; preds = %cond.false, %cond.true + %38 = bitcast %"struct.std::__1::atomic"* %obj to %"struct.std::__1::__atomic_base.0"* + store %"struct.std::__1::__atomic_base.0"* %38, %"struct.std::__1::__atomic_base.0"** %this.addr.i82, align 4 + %this1.i83 = load %"struct.std::__1::__atomic_base.0"** %this.addr.i82 + store %"struct.std::__1::__atomic_base.0"* %this1.i83, %"struct.std::__1::__atomic_base.0"** %this.addr.i.i79, align 4 + store i32 5, i32* %__m.addr.i.i80, align 4 + %this1.i.i84 = load %"struct.std::__1::__atomic_base.0"** %this.addr.i.i79 + %__a_.i.i85 = getelementptr inbounds %"struct.std::__1::__atomic_base.0"* %this1.i.i84, i32 0, i32 0 + %39 = load i32* %__m.addr.i.i80, align 4 + switch i32 %39, label %monotonic.i.i86 [ + i32 1, label %acquire.i.i87 + i32 2, label %acquire.i.i87 + i32 5, label %seqcst.i.i88 + ] + +monotonic.i.i86: ; preds = %cond.end + %40 = load atomic volatile i8* %__a_.i.i85 monotonic, align 1 + store i8 %40, i8* %.atomicdst.i.i81, align 1 + br label %_ZNVKSt3__113__atomic_baseIcLb0EEcvcEv.exit89 + +acquire.i.i87: ; preds = %cond.end, %cond.end + %41 = load atomic volatile i8* %__a_.i.i85 acquire, align 1 + store i8 %41, i8* %.atomicdst.i.i81, align 1 + br label %_ZNVKSt3__113__atomic_baseIcLb0EEcvcEv.exit89 + +seqcst.i.i88: ; preds = %cond.end + %42 = load atomic volatile i8* %__a_.i.i85 seq_cst, align 1 + store i8 %42, i8* %.atomicdst.i.i81, align 1 + br label %_ZNVKSt3__113__atomic_baseIcLb0EEcvcEv.exit89 + +_ZNVKSt3__113__atomic_baseIcLb0EEcvcEv.exit89: ; preds = %seqcst.i.i88, %acquire.i.i87, %monotonic.i.i86 + %43 = load i8* %.atomicdst.i.i81 + %conv3 = sext i8 %43 to i32 + %cmp4 = icmp eq i32 %conv3, 2 + br i1 %cmp4, label %cond.true5, label %cond.false6 + +cond.true5: ; preds = %_ZNVKSt3__113__atomic_baseIcLb0EEcvcEv.exit89 + br label %cond.end7 + +cond.false6: ; preds = %_ZNVKSt3__113__atomic_baseIcLb0EEcvcEv.exit89 + call void @__assert_func(i8* getelementptr inbounds ([8 x i8]* @.str, i32 0, i32 0), i32 22, i8* getelementptr inbounds ([63 x i8]* @__PRETTY_FUNCTION__._Z7do_testIVNSt3__16atomicIcEEcEvv, i32 0, i32 0), i8* getelementptr inbounds ([12 x i8]* @.str2, i32 0, i32 0)) + br label %cond.end7 + +cond.end7: ; preds = %cond.false6, %cond.true5 + %44 = load i8* %x, align 1 + %conv8 = sext i8 %44 to i32 + %cmp9 = icmp eq i32 %conv8, 3 + br i1 %cmp9, label %cond.true10, label %cond.false11 + +cond.true10: ; preds = %cond.end7 + br label %cond.end12 + +cond.false11: ; preds = %cond.end7 + call void @__assert_func(i8* getelementptr inbounds ([8 x i8]* @.str, i32 0, i32 0), i32 23, i8* getelementptr inbounds ([63 x i8]* @__PRETTY_FUNCTION__._Z7do_testIVNSt3__16atomicIcEEcEvv, i32 0, i32 0), i8* getelementptr inbounds ([10 x i8]* @.str3, i32 0, i32 0)) + br label %cond.end12 + +cond.end12: ; preds = %cond.false11, %cond.true10 + %45 = bitcast %"struct.std::__1::atomic"* %obj to %"struct.std::__1::__atomic_base.0"* + store %"struct.std::__1::__atomic_base.0"* %45, %"struct.std::__1::__atomic_base.0"** %this.addr.i60, align 4 + store i8* %x, i8** %__e.addr.i61, align 4 + store i8 1, i8* %__d.addr.i62, align 1 + store i32 5, i32* %__m.addr.i63, align 4 + %this1.i66 = load %"struct.std::__1::__atomic_base.0"** %this.addr.i60 + %__a_.i67 = getelementptr inbounds %"struct.std::__1::__atomic_base.0"* %this1.i66, i32 0, i32 0 + %46 = load i32* %__m.addr.i63, align 4 + %47 = load i8** %__e.addr.i61, align 4 + %48 = load i8* %__d.addr.i62, align 1 + store i8 %48, i8* %.atomictmp.i64 + %49 = load i32* %__m.addr.i63, align 4 + switch i32 %46, label %monotonic.i69 [ + i32 1, label %acquire.i71 + i32 2, label %acquire.i71 + i32 3, label %release.i73 + i32 4, label %acqrel.i75 + i32 5, label %seqcst.i77 + ] + +monotonic.i69: ; preds = %cond.end12 + %50 = load i8* %47, align 1 + %51 = load i8* %.atomictmp.i64, align 1 + %52 = cmpxchg volatile i8* %__a_.i67, i8 %50, i8 %51 monotonic + store i8 %52, i8* %47, align 1 + %53 = icmp eq i8 %52, %50 + %frombool.i68 = zext i1 %53 to i8 + store i8 %frombool.i68, i8* %.atomicdst.i65 + br label %_ZNVSt3__113__atomic_baseIcLb0EE21compare_exchange_weakERccNS_12memory_orderE.exit + +acquire.i71: ; preds = %cond.end12, %cond.end12 + %54 = load i8* %47, align 1 + %55 = load i8* %.atomictmp.i64, align 1 + %56 = cmpxchg volatile i8* %__a_.i67, i8 %54, i8 %55 acquire + store i8 %56, i8* %47, align 1 + %57 = icmp eq i8 %56, %54 + %frombool2.i70 = zext i1 %57 to i8 + store i8 %frombool2.i70, i8* %.atomicdst.i65 + br label %_ZNVSt3__113__atomic_baseIcLb0EE21compare_exchange_weakERccNS_12memory_orderE.exit + +release.i73: ; preds = %cond.end12 + %58 = load i8* %47, align 1 + %59 = load i8* %.atomictmp.i64, align 1 + %60 = cmpxchg volatile i8* %__a_.i67, i8 %58, i8 %59 release + store i8 %60, i8* %47, align 1 + %61 = icmp eq i8 %60, %58 + %frombool3.i72 = zext i1 %61 to i8 + store i8 %frombool3.i72, i8* %.atomicdst.i65 + br label %_ZNVSt3__113__atomic_baseIcLb0EE21compare_exchange_weakERccNS_12memory_orderE.exit + +acqrel.i75: ; preds = %cond.end12 + %62 = load i8* %47, align 1 + %63 = load i8* %.atomictmp.i64, align 1 + %64 = cmpxchg volatile i8* %__a_.i67, i8 %62, i8 %63 acq_rel + store i8 %64, i8* %47, align 1 + %65 = icmp eq i8 %64, %62 + %frombool4.i74 = zext i1 %65 to i8 + store i8 %frombool4.i74, i8* %.atomicdst.i65 + br label %_ZNVSt3__113__atomic_baseIcLb0EE21compare_exchange_weakERccNS_12memory_orderE.exit + +seqcst.i77: ; preds = %cond.end12 + %66 = load i8* %47, align 1 + %67 = load i8* %.atomictmp.i64, align 1 + %68 = cmpxchg volatile i8* %__a_.i67, i8 %66, i8 %67 seq_cst + store i8 %68, i8* %47, align 1 + %69 = icmp eq i8 %68, %66 + %frombool5.i76 = zext i1 %69 to i8 + store i8 %frombool5.i76, i8* %.atomicdst.i65 + br label %_ZNVSt3__113__atomic_baseIcLb0EE21compare_exchange_weakERccNS_12memory_orderE.exit + +_ZNVSt3__113__atomic_baseIcLb0EE21compare_exchange_weakERccNS_12memory_orderE.exit: ; preds = %seqcst.i77, %acqrel.i75, %release.i73, %acquire.i71, %monotonic.i69 + %70 = load i8* %.atomicdst.i65 + %tobool.i78 = trunc i8 %70 to i1 + %conv14 = zext i1 %tobool.i78 to i32 + %cmp15 = icmp eq i32 %conv14, 0 + br i1 %cmp15, label %cond.true16, label %cond.false17 + +cond.true16: ; preds = %_ZNVSt3__113__atomic_baseIcLb0EE21compare_exchange_weakERccNS_12memory_orderE.exit + br label %cond.end18 + +cond.false17: ; preds = %_ZNVSt3__113__atomic_baseIcLb0EE21compare_exchange_weakERccNS_12memory_orderE.exit + call void @__assert_func(i8* getelementptr inbounds ([8 x i8]* @.str, i32 0, i32 0), i32 24, i8* getelementptr inbounds ([63 x i8]* @__PRETTY_FUNCTION__._Z7do_testIVNSt3__16atomicIcEEcEvv, i32 0, i32 0), i8* getelementptr inbounds ([44 x i8]* @.str4, i32 0, i32 0)) + br label %cond.end18 + +cond.end18: ; preds = %cond.false17, %cond.true16 + %71 = bitcast %"struct.std::__1::atomic"* %obj to %"struct.std::__1::__atomic_base.0"* + store %"struct.std::__1::__atomic_base.0"* %71, %"struct.std::__1::__atomic_base.0"** %this.addr.i52, align 4 + %this1.i53 = load %"struct.std::__1::__atomic_base.0"** %this.addr.i52 + store %"struct.std::__1::__atomic_base.0"* %this1.i53, %"struct.std::__1::__atomic_base.0"** %this.addr.i.i49, align 4 + store i32 5, i32* %__m.addr.i.i50, align 4 + %this1.i.i54 = load %"struct.std::__1::__atomic_base.0"** %this.addr.i.i49 + %__a_.i.i55 = getelementptr inbounds %"struct.std::__1::__atomic_base.0"* %this1.i.i54, i32 0, i32 0 + %72 = load i32* %__m.addr.i.i50, align 4 + switch i32 %72, label %monotonic.i.i56 [ + i32 1, label %acquire.i.i57 + i32 2, label %acquire.i.i57 + i32 5, label %seqcst.i.i58 + ] + +monotonic.i.i56: ; preds = %cond.end18 + %73 = load atomic volatile i8* %__a_.i.i55 monotonic, align 1 + store i8 %73, i8* %.atomicdst.i.i51, align 1 + br label %_ZNVKSt3__113__atomic_baseIcLb0EEcvcEv.exit59 + +acquire.i.i57: ; preds = %cond.end18, %cond.end18 + %74 = load atomic volatile i8* %__a_.i.i55 acquire, align 1 + store i8 %74, i8* %.atomicdst.i.i51, align 1 + br label %_ZNVKSt3__113__atomic_baseIcLb0EEcvcEv.exit59 + +seqcst.i.i58: ; preds = %cond.end18 + %75 = load atomic volatile i8* %__a_.i.i55 seq_cst, align 1 + store i8 %75, i8* %.atomicdst.i.i51, align 1 + br label %_ZNVKSt3__113__atomic_baseIcLb0EEcvcEv.exit59 + +_ZNVKSt3__113__atomic_baseIcLb0EEcvcEv.exit59: ; preds = %seqcst.i.i58, %acquire.i.i57, %monotonic.i.i56 + %76 = load i8* %.atomicdst.i.i51 + %conv20 = sext i8 %76 to i32 + %cmp21 = icmp eq i32 %conv20, 2 + br i1 %cmp21, label %cond.true22, label %cond.false23 + +cond.true22: ; preds = %_ZNVKSt3__113__atomic_baseIcLb0EEcvcEv.exit59 + br label %cond.end24 + +cond.false23: ; preds = %_ZNVKSt3__113__atomic_baseIcLb0EEcvcEv.exit59 + call void @__assert_func(i8* getelementptr inbounds ([8 x i8]* @.str, i32 0, i32 0), i32 25, i8* getelementptr inbounds ([63 x i8]* @__PRETTY_FUNCTION__._Z7do_testIVNSt3__16atomicIcEEcEvv, i32 0, i32 0), i8* getelementptr inbounds ([12 x i8]* @.str2, i32 0, i32 0)) + br label %cond.end24 + +cond.end24: ; preds = %cond.false23, %cond.true22 + %77 = load i8* %x, align 1 + %conv25 = sext i8 %77 to i32 + %cmp26 = icmp eq i32 %conv25, 2 + br i1 %cmp26, label %cond.true27, label %cond.false28 + +cond.true27: ; preds = %cond.end24 + br label %cond.end29 + +cond.false28: ; preds = %cond.end24 + call void @__assert_func(i8* getelementptr inbounds ([8 x i8]* @.str, i32 0, i32 0), i32 26, i8* getelementptr inbounds ([63 x i8]* @__PRETTY_FUNCTION__._Z7do_testIVNSt3__16atomicIcEEcEvv, i32 0, i32 0), i8* getelementptr inbounds ([10 x i8]* @.str5, i32 0, i32 0)) + br label %cond.end29 + +cond.end29: ; preds = %cond.false28, %cond.true27 + store i8 2, i8* %x, align 1 + %78 = bitcast %"struct.std::__1::atomic"* %obj to %"struct.std::__1::__atomic_base.0"* + store %"struct.std::__1::__atomic_base.0"* %78, %"struct.std::__1::__atomic_base.0"** %this.addr.i46, align 4 + store i8* %x, i8** %__e.addr.i, align 4 + store i8 1, i8* %__d.addr.i47, align 1 + store i32 5, i32* %__m.addr.i, align 4 + %this1.i48 = load %"struct.std::__1::__atomic_base.0"** %this.addr.i46 + %__a_.i = getelementptr inbounds %"struct.std::__1::__atomic_base.0"* %this1.i48, i32 0, i32 0 + %79 = load i32* %__m.addr.i, align 4 + %80 = load i8** %__e.addr.i, align 4 + %81 = load i8* %__d.addr.i47, align 1 + store i8 %81, i8* %.atomictmp.i + %82 = load i32* %__m.addr.i, align 4 + switch i32 %79, label %monotonic.i [ + i32 1, label %acquire.i + i32 2, label %acquire.i + i32 3, label %release.i + i32 4, label %acqrel.i + i32 5, label %seqcst.i + ] + +monotonic.i: ; preds = %cond.end29 + %83 = load i8* %80, align 1 + %84 = load i8* %.atomictmp.i, align 1 + %85 = cmpxchg volatile i8* %__a_.i, i8 %83, i8 %84 monotonic + store i8 %85, i8* %80, align 1 + %86 = icmp eq i8 %85, %83 + %frombool.i = zext i1 %86 to i8 + store i8 %frombool.i, i8* %.atomicdst.i + br label %_ZNVSt3__113__atomic_baseIcLb0EE23compare_exchange_strongERccNS_12memory_orderE.exit + +acquire.i: ; preds = %cond.end29, %cond.end29 + %87 = load i8* %80, align 1 + %88 = load i8* %.atomictmp.i, align 1 + %89 = cmpxchg volatile i8* %__a_.i, i8 %87, i8 %88 acquire + store i8 %89, i8* %80, align 1 + %90 = icmp eq i8 %89, %87 + %frombool2.i = zext i1 %90 to i8 + store i8 %frombool2.i, i8* %.atomicdst.i + br label %_ZNVSt3__113__atomic_baseIcLb0EE23compare_exchange_strongERccNS_12memory_orderE.exit + +release.i: ; preds = %cond.end29 + %91 = load i8* %80, align 1 + %92 = load i8* %.atomictmp.i, align 1 + %93 = cmpxchg volatile i8* %__a_.i, i8 %91, i8 %92 release + store i8 %93, i8* %80, align 1 + %94 = icmp eq i8 %93, %91 + %frombool3.i = zext i1 %94 to i8 + store i8 %frombool3.i, i8* %.atomicdst.i + br label %_ZNVSt3__113__atomic_baseIcLb0EE23compare_exchange_strongERccNS_12memory_orderE.exit + +acqrel.i: ; preds = %cond.end29 + %95 = load i8* %80, align 1 + %96 = load i8* %.atomictmp.i, align 1 + %97 = cmpxchg volatile i8* %__a_.i, i8 %95, i8 %96 acq_rel + store i8 %97, i8* %80, align 1 + %98 = icmp eq i8 %97, %95 + %frombool4.i = zext i1 %98 to i8 + store i8 %frombool4.i, i8* %.atomicdst.i + br label %_ZNVSt3__113__atomic_baseIcLb0EE23compare_exchange_strongERccNS_12memory_orderE.exit + +seqcst.i: ; preds = %cond.end29 + %99 = load i8* %80, align 1 + %100 = load i8* %.atomictmp.i, align 1 + %101 = cmpxchg volatile i8* %__a_.i, i8 %99, i8 %100 seq_cst + store i8 %101, i8* %80, align 1 + %102 = icmp eq i8 %101, %99 + %frombool5.i = zext i1 %102 to i8 + store i8 %frombool5.i, i8* %.atomicdst.i + br label %_ZNVSt3__113__atomic_baseIcLb0EE23compare_exchange_strongERccNS_12memory_orderE.exit + +_ZNVSt3__113__atomic_baseIcLb0EE23compare_exchange_strongERccNS_12memory_orderE.exit: ; preds = %seqcst.i, %acqrel.i, %release.i, %acquire.i, %monotonic.i + %103 = load i8* %.atomicdst.i + %tobool.i = trunc i8 %103 to i1 + %conv31 = zext i1 %tobool.i to i32 + %cmp32 = icmp eq i32 %conv31, 1 + br i1 %cmp32, label %cond.true33, label %cond.false34 + +cond.true33: ; preds = %_ZNVSt3__113__atomic_baseIcLb0EE23compare_exchange_strongERccNS_12memory_orderE.exit + br label %cond.end35 + +cond.false34: ; preds = %_ZNVSt3__113__atomic_baseIcLb0EE23compare_exchange_strongERccNS_12memory_orderE.exit + call void @__assert_func(i8* getelementptr inbounds ([8 x i8]* @.str, i32 0, i32 0), i32 28, i8* getelementptr inbounds ([63 x i8]* @__PRETTY_FUNCTION__._Z7do_testIVNSt3__16atomicIcEEcEvv, i32 0, i32 0), i8* getelementptr inbounds ([45 x i8]* @.str6, i32 0, i32 0)) + br label %cond.end35 + +cond.end35: ; preds = %cond.false34, %cond.true33 + %104 = bitcast %"struct.std::__1::atomic"* %obj to %"struct.std::__1::__atomic_base.0"* + store %"struct.std::__1::__atomic_base.0"* %104, %"struct.std::__1::__atomic_base.0"** %this.addr.i43, align 4 + %this1.i44 = load %"struct.std::__1::__atomic_base.0"** %this.addr.i43 + store %"struct.std::__1::__atomic_base.0"* %this1.i44, %"struct.std::__1::__atomic_base.0"** %this.addr.i.i42, align 4 + store i32 5, i32* %__m.addr.i.i, align 4 + %this1.i.i45 = load %"struct.std::__1::__atomic_base.0"** %this.addr.i.i42 + %__a_.i.i = getelementptr inbounds %"struct.std::__1::__atomic_base.0"* %this1.i.i45, i32 0, i32 0 + %105 = load i32* %__m.addr.i.i, align 4 + switch i32 %105, label %monotonic.i.i [ + i32 1, label %acquire.i.i + i32 2, label %acquire.i.i + i32 5, label %seqcst.i.i + ] + +monotonic.i.i: ; preds = %cond.end35 + %106 = load atomic volatile i8* %__a_.i.i monotonic, align 1 + store i8 %106, i8* %.atomicdst.i.i, align 1 + br label %_ZNVKSt3__113__atomic_baseIcLb0EEcvcEv.exit + +acquire.i.i: ; preds = %cond.end35, %cond.end35 + %107 = load atomic volatile i8* %__a_.i.i acquire, align 1 + store i8 %107, i8* %.atomicdst.i.i, align 1 + br label %_ZNVKSt3__113__atomic_baseIcLb0EEcvcEv.exit + +seqcst.i.i: ; preds = %cond.end35 + %108 = load atomic volatile i8* %__a_.i.i seq_cst, align 1 + store i8 %108, i8* %.atomicdst.i.i, align 1 + br label %_ZNVKSt3__113__atomic_baseIcLb0EEcvcEv.exit + +_ZNVKSt3__113__atomic_baseIcLb0EEcvcEv.exit: ; preds = %seqcst.i.i, %acquire.i.i, %monotonic.i.i + %109 = load i8* %.atomicdst.i.i + %conv37 = sext i8 %109 to i32 + %cmp38 = icmp eq i32 %conv37, 1 + br i1 %cmp38, label %cond.true39, label %cond.false40 + +cond.true39: ; preds = %_ZNVKSt3__113__atomic_baseIcLb0EEcvcEv.exit + br label %cond.end41 + +cond.false40: ; preds = %_ZNVKSt3__113__atomic_baseIcLb0EEcvcEv.exit + call void @__assert_func(i8* getelementptr inbounds ([8 x i8]* @.str, i32 0, i32 0), i32 29, i8* getelementptr inbounds ([63 x i8]* @__PRETTY_FUNCTION__._Z7do_testIVNSt3__16atomicIcEEcEvv, i32 0, i32 0), i8* getelementptr inbounds ([12 x i8]* @.str7, i32 0, i32 0)) + br label %cond.end41 + +cond.end41: ; preds = %cond.false40, %cond.true39 + ret void +} + +declare void @__assert_func(i8*, i32, i8*, i8*) +declare i32 @printf(i8*, ...) + diff --git a/tests/cases/emptystruct.ll b/tests/cases/emptystruct.ll index d4165fdd..67967e65 100644 --- a/tests/cases/emptystruct.ll +++ b/tests/cases/emptystruct.ll @@ -11,7 +11,7 @@ entry: %z = alloca %struct.s, align 4 %0 = bitcast %struct.s* %z to i8* call void @llvm.memset.p0i8.i32(i8* %0, i8 0, i32 4, i32 4, i1 false) - %0 = call i32 bitcast (i32 (i8*)* @puts to i32 (i32*)*)(i8* getelementptr inbounds ([14 x i8]* @.str, i32 0, i32 0)) ; [#uses=0] + %1 = call i32 bitcast (i32 (i8*)* @puts to i32 (i32*)*)(i8* getelementptr inbounds ([14 x i8]* @.str, i32 0, i32 0)) ; [#uses=0] ret i32 0 } diff --git a/tests/cases/gepoverflow.ll b/tests/cases/gepoverflow.ll index 315e9100..d09afd87 100644 --- a/tests/cases/gepoverflow.ll +++ b/tests/cases/gepoverflow.ll @@ -26,7 +26,7 @@ entry: %3 = sub i32 %1, %baseint ; [#uses=1] %4 = sub i32 %2, %baseint ; [#uses=1] %5 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([9 x i8]* @.str, i32 0, i32 0), i32 %1, i32 %2) ; [#uses=0] - %5 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([9 x i8]* @.str, i32 0, i32 0), i32 %3, i32 %4) ; [#uses=0] + %6 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([9 x i8]* @.str, i32 0, i32 0), i32 %3, i32 %4) ; [#uses=0] br label %return return: ; preds = %entry diff --git a/tests/cases/inttoptrfloat.ll b/tests/cases/inttoptrfloat.ll new file mode 100644 index 00000000..c3349fc4 --- /dev/null +++ b/tests/cases/inttoptrfloat.ll @@ -0,0 +1,20 @@ +; ModuleID = 'tests/hello_world.bc' +target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32-S128" +target triple = "i386-pc-linux-gnu" + +@.str = private unnamed_addr constant [15 x i8] c"hello, world!\0A\00", align 1 ; [#uses=1 type=[15 x i8]*] + +; [#uses=0] +define i32 @main() { +entry: + %call = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([15 x i8]* @.str, i32 0, i32 0)) ; [#uses=0 type=i32] + %ff = alloca float, align 4 + %a = load float* inttoptr (i32 4 to float*), align 4 + store float %a, float* %ff, align 4 + %b = load float* %ff, align 4 + %call2 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([15 x i8]* @.str, i32 0, i32 0), float %b) ; [#uses=0 type=i32] + ret i32 1 +} + +; [#uses=1] +declare i32 @printf(i8*, ...) diff --git a/tests/cases/invokeundef.ll b/tests/cases/invokeundef.ll new file mode 100644 index 00000000..9dc1f93d --- /dev/null +++ b/tests/cases/invokeundef.ll @@ -0,0 +1,41 @@ +; ModuleID = '/dev/shm/tmp/src.cpp.o' +; Just test for compilation here +target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32-f128:128:128-n8:16:32" +target triple = "i386-pc-linux-gnu" + +%struct.CPU_Regs = type { [8 x %union.GenReg32] } +%union.GenReg32 = type { [1 x i32] } + +@cpu_regs = unnamed_addr global %struct.CPU_Regs zeroinitializer, align 32 ; [#uses=2] +@.str = private unnamed_addr constant [14 x i8] c"hello, world!\00", align 1 ; [#uses=1] + +; [#uses=0] +define i32 @main() { +entry: + %retval = alloca i32 ; [#uses=2] + %0 = alloca i32 ; [#uses=2] + %"alloca point" = bitcast i32 0 to i32 ; [#uses=0] + %1 = load i32* bitcast (i32* getelementptr inbounds (%struct.CPU_Regs* @cpu_regs, i32 0, i32 0, i32 1, i32 0, i32 0) to i32*), align 2 ; [#uses=1] + store i16 %1, i16* bitcast (%struct.CPU_Regs* @cpu_regs to i16*), align 2 + %2 = call i32 @puts(i8* getelementptr inbounds ([14 x i8]* @.str, i32 0, i32 0)) ; [#uses=0] + store i32 0, i32* %0, align 4 + %3 = load i32* %0, align 4 ; [#uses=1] + store i32 %3, i32* %retval, align 4 + br label %return + + invoke void undef(%struct.CPU_Regs* noalias @cpu_regs, i32 %99) + to label %invcont33 unwind label %lpad106 + +invcont33: + ret i32 %retval1 + +lpad106: + ret i32 %retval1 + +return: ; preds = %entry + %retval1 = load i32* %retval ; [#uses=1] + ret i32 %retval1 +} + +; [#uses=1] +declare i32 @puts(i8*) diff --git a/tests/cases/longjmp_tiny_noasm_invoke.ll b/tests/cases/longjmp_tiny_noasm_invoke.ll new file mode 100644 index 00000000..e1a72e00 --- /dev/null +++ b/tests/cases/longjmp_tiny_noasm_invoke.ll @@ -0,0 +1,71 @@ +; ModuleID = '/tmp/emscripten_temp/src.cpp.o' +target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32-S128" +target triple = "i386-pc-linux-gnu" + +@_ZL3buf = internal global [20 x i16] zeroinitializer, align 2 +@.str = private unnamed_addr constant [13 x i8] c"hello world\0A\00", align 1 +@.str1 = private unnamed_addr constant [6 x i8] c"more\0A\00", align 1 + +define i32 @main() { + %retval = alloca i32, align 4 + store i32 0, i32* %retval + %call = invoke i32 @setjmp(i16* getelementptr inbounds ([20 x i16]* @_ZL3buf, i32 0, i32 0)) returns_twice, !dbg !20 + to label %allgood unwind label %awful + +allgood: + %tobool = icmp ne i32 %call, 0, !dbg !20 + br i1 %tobool, label %if.else, label %if.then, !dbg !20 + +if.then: ; preds = %entry + %call1 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([13 x i8]* @.str, i32 0, i32 0)), !dbg !22 + call void @longjmp(i16* getelementptr inbounds ([20 x i16]* @_ZL3buf, i32 0, i32 0), i32 10), !dbg !24 + br label %if.end, !dbg !25 + +if.else: ; preds = %entry + %call2 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([6 x i8]* @.str1, i32 0, i32 0)), !dbg !26 + br label %if.end + +if.end: ; preds = %if.else, %if.then + ret i32 0, !dbg !28 + +awful: + ret i32 1 +} + +declare i32 @setjmp(i16*) returns_twice + +declare i32 @printf(i8*, ...) + +declare void @longjmp(i16*, i32) + +!llvm.dbg.cu = !{!0} + +!0 = metadata !{i32 786449, i32 0, i32 4, metadata !"/tmp/emscripten_temp/src.cpp", metadata !"/home/alon/Dev/emscripten", metadata !"clang version 3.1 (trunk 150936)", i1 true, i1 false, metadata !"", i32 0, metadata !1, metadata !1, metadata !3, metadata !12} ; [ DW_TAG_compile_unit ] +!1 = metadata !{metadata !2} +!2 = metadata !{i32 0} +!3 = metadata !{metadata !4} +!4 = metadata !{metadata !5} +!5 = metadata !{i32 786478, i32 0, metadata !6, metadata !"main", metadata !"main", metadata !"", metadata !6, i32 7, metadata !7, i1 false, i1 true, i32 0, i32 0, null, i32 256, i1 false, i32 ()* @main, null, null, metadata !10} ; [ DW_TAG_subprogram ] +!6 = metadata !{i32 786473, metadata !"/tmp/emscripten_temp/src.cpp", metadata !"/home/alon/Dev/emscripten", null} ; [ DW_TAG_file_type ] +!7 = metadata !{i32 786453, i32 0, metadata !"", i32 0, i32 0, i64 0, i64 0, i64 0, i32 0, null, metadata !8, i32 0, i32 0} ; [ DW_TAG_subroutine_type ] +!8 = metadata !{metadata !9} +!9 = metadata !{i32 786468, null, metadata !"int", null, i32 0, i64 32, i64 32, i64 0, i32 0, i32 5} ; [ DW_TAG_base_type ] +!10 = metadata !{metadata !11} +!11 = metadata !{i32 786468} ; [ DW_TAG_base_type ] +!12 = metadata !{metadata !13} +!13 = metadata !{metadata !14} +!14 = metadata !{i32 786484, i32 0, null, metadata !"buf", metadata !"buf", metadata !"_ZL3buf", metadata !6, i32 5, metadata !15, i32 1, i32 1, [20 x i16]* @_ZL3buf} ; [ DW_TAG_variable ] +!15 = metadata !{i32 786454, null, metadata !"jmp_buf", metadata !6, i32 279, i64 0, i64 0, i64 0, i32 0, metadata !16} ; [ DW_TAG_typedef ] +!16 = metadata !{i32 786433, null, metadata !"", null, i32 0, i64 320, i64 16, i32 0, i32 0, metadata !17, metadata !18, i32 0, i32 0} ; [ DW_TAG_array_type ] +!17 = metadata !{i32 786468, null, metadata !"unsigned short", null, i32 0, i64 16, i64 16, i64 0, i32 0, i32 7} ; [ DW_TAG_base_type ] +!18 = metadata !{metadata !19} +!19 = metadata !{i32 786465, i64 0, i64 19} ; [ DW_TAG_subrange_type ] +!20 = metadata !{i32 8, i32 18, metadata !21, null} +!21 = metadata !{i32 786443, metadata !5, i32 7, i32 22, metadata !6, i32 0} ; [ DW_TAG_lexical_block ] +!22 = metadata !{i32 9, i32 15, metadata !23, null} +!23 = metadata !{i32 786443, metadata !21, i32 8, i32 31, metadata !6, i32 1} ; [ DW_TAG_lexical_block ] +!24 = metadata !{i32 10, i32 15, metadata !23, null} +!25 = metadata !{i32 11, i32 13, metadata !23, null} +!26 = metadata !{i32 12, i32 15, metadata !27, null} +!27 = metadata !{i32 786443, metadata !21, i32 11, i32 20, metadata !6, i32 2} ; [ DW_TAG_lexical_block ] +!28 = metadata !{i32 14, i32 13, metadata !21, null} diff --git a/tests/cases/longjmp_tiny_noasm_invoke.txt b/tests/cases/longjmp_tiny_noasm_invoke.txt new file mode 100644 index 00000000..8a0aa386 --- /dev/null +++ b/tests/cases/longjmp_tiny_noasm_invoke.txt @@ -0,0 +1,2 @@ +hello world +more diff --git a/tests/cases/storestruct.ll b/tests/cases/storestruct.ll index 15022e2f..5bd9224e 100644 --- a/tests/cases/storestruct.ll +++ b/tests/cases/storestruct.ll @@ -42,14 +42,14 @@ entry: %tmp5 = load i32* %a1, align 4, !dbg !18 ; [#uses=1] %tmp6 = load i32* %b2, align 4, !dbg !18 ; [#uses=1] - %call = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([9 x i8]* @.str, i32 0, i32 0), i32 %tmp5, i32 %tmp6), !dbg !18 ; [#uses=0] + %call2 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([9 x i8]* @.str, i32 0, i32 0), i32 %tmp5, i32 %tmp6), !dbg !18 ; [#uses=0] %ptr = inttoptr i32 52 to i32* ; [#uses=1] store %struct.X { i32 ptrtoint (i32* getelementptr inbounds (i32* %ptr, i32 1, i32 0) to i32), i32 3 }, %struct.X* %y, align 4 ; store entire struct at once - %tmp5 = load i32* %a1, align 4, !dbg !18 ; [#uses=1] - %tmp6 = load i32* %b2, align 4, !dbg !18 ; [#uses=1] - %call = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([9 x i8]* @.str, i32 0, i32 0), i32 %tmp5, i32 %tmp6), !dbg !18 ; [#uses=0] + %tmp5b = load i32* %a1, align 4, !dbg !18 ; [#uses=1] + %tmp6b = load i32* %b2, align 4, !dbg !18 ; [#uses=1] + %call3 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([9 x i8]* @.str, i32 0, i32 0), i32 %tmp5b, i32 %tmp6b), !dbg !18 ; [#uses=0] ret i32 0, !dbg !19 } diff --git a/tests/cases/udiv.ll b/tests/cases/udiv.ll new file mode 100644 index 00000000..a809db5a --- /dev/null +++ b/tests/cases/udiv.ll @@ -0,0 +1,19 @@ +; ModuleID = 'tests/hello_world.bc' +target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32-S128" +target triple = "i386-pc-linux-gnu" + +@.str = private unnamed_addr constant [15 x i8] c"hello, world!\0A\00", align 1 ; [#uses=1 type=[15 x i8]*] + +; [#uses=0] +define i32 @main() { +entry: + %retval = alloca i32, align 4 ; [#uses=1 type=i32*] + %waka = udiv i32 -1806866064, 5 + %waka2 = add i32 2247483648, 2247483648 + store i32 0, i32* %retval + %call = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([15 x i8]* @.str, i32 0, i32 0), i32 %waka, i32 %waka2) ; [#uses=0 type=i32] + ret i32 1 +} + +; [#uses=1] +declare i32 @printf(i8*, ...) diff --git a/tests/cubegeom.c b/tests/cubegeom.c index c137ad80..787b8373 100644 --- a/tests/cubegeom.c +++ b/tests/cubegeom.c @@ -45,6 +45,8 @@ void verify() { int main(int argc, char *argv[]) { + int temp; // testing + SDL_Surface *screen; if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) { printf("Unable to initialize SDL: %s\n", SDL_GetError()); @@ -124,7 +126,9 @@ int main(int argc, char *argv[]) glActiveTexture(GL_TEXTURE0); + glGetBooleanv(GL_VERTEX_ARRAY, &temp); assert(!temp); glEnableClientState(GL_VERTEX_ARRAY); + glGetBooleanv(GL_VERTEX_ARRAY, &temp); assert(temp); GLuint arrayBuffer, elementBuffer; glGenBuffers(1, &arrayBuffer); @@ -207,6 +211,20 @@ int main(int argc, char *argv[]) glNormalPointer(GL_BYTE, 32, (void*)12); glColorPointer(4, GL_UNSIGNED_BYTE, 32, (void*)28); + glGetPointerv(GL_VERTEX_ARRAY_POINTER, &temp); assert(temp == 0); + glGetPointerv(GL_COLOR_ARRAY_POINTER, &temp); assert(temp == 28); + glGetPointerv(GL_TEXTURE_COORD_ARRAY_POINTER, &temp); assert(temp == 16); + glGetIntegerv(GL_VERTEX_ARRAY_SIZE, &temp); assert(temp == 3); + glGetIntegerv(GL_VERTEX_ARRAY_TYPE, &temp); assert(temp == GL_FLOAT); + glGetIntegerv(GL_VERTEX_ARRAY_STRIDE, &temp); assert(temp == 32); + glGetIntegerv(GL_COLOR_ARRAY_SIZE, &temp); assert(temp == 4); + glGetIntegerv(GL_COLOR_ARRAY_TYPE, &temp); assert(temp == GL_UNSIGNED_BYTE); + glGetIntegerv(GL_COLOR_ARRAY_STRIDE, &temp); assert(temp == 32); + glGetIntegerv(GL_TEXTURE_COORD_ARRAY_SIZE, &temp); assert(temp == 2); + glGetIntegerv(GL_TEXTURE_COORD_ARRAY_TYPE, &temp); assert(temp == GL_FLOAT); + glGetIntegerv(GL_TEXTURE_COORD_ARRAY_STRIDE, &temp); assert(temp == 32); + glGetBooleanv(GL_VERTEX_ARRAY, &temp); assert(temp); + glBindTexture(GL_TEXTURE_2D, texture); // diffuse? glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE1); diff --git a/tests/cubegeom_pre2_vao.c b/tests/cubegeom_pre2_vao.c new file mode 100644 index 00000000..98bedd54 --- /dev/null +++ b/tests/cubegeom_pre2_vao.c @@ -0,0 +1,380 @@ +/* +THIS WORK, INCLUDING THE SOURCE CODE, DOCUMENTATION +AND RELATED MEDIA AND DATA, IS PLACED INTO THE PUBLIC DOMAIN. + +THE ORIGINAL AUTHOR IS KYLE FOLEY. + +THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY +OF ANY KIND, NOT EVEN THE IMPLIED WARRANTY OF +MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE, +ASSUMES _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE +RESULTING FROM THE USE, MODIFICATION, OR +REDISTRIBUTION OF THIS SOFTWARE. +*/ + +#if !EMSCRIPTEN +#define USE_GLEW 1 +#endif + +#if USE_GLEW +#include "GL/glew.h" +#endif + +#include "SDL/SDL.h" +#if !USE_GLEW +#include "SDL/SDL_opengl.h" +#endif + +#include <stdio.h> +#include <string.h> +#include <assert.h> + +PFNGLGENVERTEXARRAYSPROC glGenVertexArrays_ = NULL; +PFNGLBINDVERTEXARRAYPROC glBindVertexArray_ = NULL; +PFNGLDELETEVERTEXARRAYSPROC glDeleteVertexArrays_ = NULL; + +void verify() { + int width = 640, height = 480; + unsigned char *data = (unsigned char*)malloc(width*height*4); + glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data); + int sum = 0; + for (int x = 0; x < width*height*4; x++) { + if (x % 4 != 3) sum += x * data[x]; + } +#if EMSCRIPTEN + int result = sum; + REPORT_RESULT(); +#endif +} + +int main(int argc, char *argv[]) +{ + SDL_Surface *screen; + if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) { + printf("Unable to initialize SDL: %s\n", SDL_GetError()); + return 1; + } + + SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); + screen = SDL_SetVideoMode( 640, 480, 24, SDL_OPENGL ); + if ( !screen ) { + printf("Unable to set video mode: %s\n", SDL_GetError()); + return 1; + } + + glClearColor( 0, 0, 0, 0 ); + glClear( GL_COLOR_BUFFER_BIT ); + + glGenVertexArrays_ = (PFNGLGENVERTEXARRAYSPROC) SDL_GL_GetProcAddress("glGenVertexArrays"); + assert(glGenVertexArrays_); + glBindVertexArray_ = (PFNGLBINDVERTEXARRAYPROC) SDL_GL_GetProcAddress("glBindVertexArray"); + assert(glBindVertexArray_); + glDeleteVertexArrays_ = (PFNGLDELETEVERTEXARRAYSPROC) SDL_GL_GetProcAddress("glDeleteVertexArrays"); + assert(glDeleteVertexArrays_); + + // Generate a VAO + GLuint vao; + glGenVertexArrays_(1, &vao); + glBindVertexArray_(vao); + + // Create a texture + + GLuint texture; + glGenTextures( 1, &texture ); + glBindTexture( GL_TEXTURE_2D, texture ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); + GLubyte textureData[16*16*4]; + for (int x = 0; x < 16; x++) { + for (int y = 0; y < 16; y++) { + *((int*)&textureData[(x*16 + y) * 4]) = x*16 + ((y*16) << 8); + } + } + glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0, + GL_RGBA, GL_UNSIGNED_BYTE, textureData ); + + // Create a second texture + + GLuint texture2; + glGenTextures( 1, &texture2 ); + glBindTexture( GL_TEXTURE_2D, texture2 ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); + GLubyte texture2Data[] = { 0xff, 0, 0, 0xff, + 0, 0xff, 0, 0xaa, + 0, 0, 0xff, 0x55, + 0x80, 0x90, 0x70, 0 }; + glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, + GL_RGBA, GL_UNSIGNED_BYTE, texture2Data ); + + // BEGIN + +#if USE_GLEW + glewInit(); +#endif + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + // original: glFrustum(-0.6435469817188064, 0.6435469817188064 ,-0.48266022190470925, 0.48266022190470925 ,0.5400000214576721, 2048); + //glFrustum(-0.6435469817188064, 0.1435469817188064 ,-0.48266022190470925, 0.88266022190470925 ,0.5400000214576721, 2048); + GLfloat pm[] = { 1.372136116027832, 0, 0, 0, 0, 0.7910231351852417, 0, 0, -0.6352481842041016, 0.29297152161598206, -1.0005275011062622, -1, 0, 0, -1.080284833908081, 0 }; + glLoadMatrixf(pm); + + glMatrixMode(GL_MODELVIEW); + GLfloat matrixData[] = { -1, 0, 0, 0, + 0, 0,-1, 0, + 0, 1, 0, 0, + 0, 0, 0, 1 }; + glLoadMatrixf(matrixData); + //glTranslated(-512,-512,-527); // XXX this should be uncommented, but if it is then nothing is shown + +// glEnable(GL_CULL_FACE); + // glEnable(GL_DEPTH_TEST); + + //glClear(GL_DEPTH_BUFFER_BIT); + +// glEnableClientState(GL_NORMAL_ARRAY); + // glEnableClientState(GL_COLOR_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + + glActiveTexture(GL_TEXTURE0); + + glEnableClientState(GL_VERTEX_ARRAY); + + GLuint arrayBuffer, elementBuffer; + glGenBuffers(1, &arrayBuffer); + glGenBuffers(1, &elementBuffer); + + GLubyte arrayData[] = { +/* +[0, 0, 0, 67] ==> 128 float +[0, 0, 128, 67] ==> 256 float +[0, 0, 0, 68] ==> 512 float +[0, 0, 128, 68] ==> 1024 float + +[vertex x ] [vertex y ] [vertex z ] [nr] [texture u ] [texture v ] [lm u ] [lm v ] [color r,g,b,a ] */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, // 0 + 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, // 1 + 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 2 + 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 3 + 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, // 4 + 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 67, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, // 5 + 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 67, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 6 + 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 7 + 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 8 + 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 9 + 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 128, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 10 + 0, 0, 0, 0, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 11 + 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 12 + 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 67, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 13 + 0, 0, 128, 68, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 67, 0, 0, 128, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 14 + 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 128, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 15 + + 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 0, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 128, 68, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128 + }; + assert(sizeof(arrayData) == 1408); + glBindBuffer(GL_ARRAY_BUFFER, arrayBuffer); + glBufferData(GL_ARRAY_BUFFER, sizeof(arrayData), arrayData, GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); + + GLushort elementData[] = { 1, 2, 0, 2, 3, 0, 5, 6, 4, 6, 7, 4, 9, 10, 8, 10, 11, 8, 13, 14, 12, 14, 15, 12 }; + assert(sizeof(elementData) == 48); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elementData), elementData, GL_STATIC_DRAW); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + + glBindBuffer(GL_ARRAY_BUFFER, arrayBuffer); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer); + + // sauer vertex data is apparently 0-12: V3F, 12: N1B, 16-24: T2F, 24-28: T2S, 28-32: C4B + glVertexPointer(3, GL_FLOAT, 32, (void*)0); // all these apply to the ARRAY_BUFFER that is bound + glTexCoordPointer(2, GL_FLOAT, 32, (void*)16); +// glClientActiveTexture(GL_TEXTURE1); // XXX seems to be ignored in native build +// glTexCoordPointer(2, GL_SHORT, 32, (void*)24); +// glClientActiveTexture(GL_TEXTURE0); // likely not needed, it is a cleanup +// glNormalPointer(GL_BYTE, 32, (void*)12); +// glColorPointer(4, GL_UNSIGNED_BYTE, 32, (void*)28); + + glBindTexture(GL_TEXTURE_2D, texture); // diffuse? + glActiveTexture(GL_TEXTURE0); + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, texture2); // lightmap? + glActiveTexture(GL_TEXTURE0); + + GLint ok; + + const char *vertexShader = "uniform mat4 u_modelView;\n" + "uniform mat4 u_projection;\n" + "varying vec4 v_texCoord0;\n" + "void main(void)\n" + "{\n" // (gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex) + // (u_projection * u_modelView * a_position) + " gl_Position = (u_projection * u_modelView * gl_Vertex) + vec4(200, 0, 0, 0);\n" + " v_texCoord0.xy = gl_MultiTexCoord0.xy/20.0;\n" // added /100 here + "}\n"; + const char *fragmentShader = "uniform sampler2D diffusemap;\n" + "varying vec4 v_texCoord0;\n" + "void main(void)\n" + "{\n" + " vec4 diffuse = texture2D(diffusemap, v_texCoord0.xy);\n" + " gl_FragColor = diffuse;\n" + "}\n"; + + GLuint vs = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vs, 1, &vertexShader, NULL); + glCompileShader(vs); + glGetShaderiv(vs, GL_COMPILE_STATUS, &ok); + if (!ok) { + printf("Shader compilation error with vertex\n"); + GLint infoLen = 0; + glGetShaderiv (vs, GL_INFO_LOG_LENGTH, &infoLen); + if (infoLen > 1) + { + char* infoLog = (char *)malloc(sizeof(char) * infoLen+1); + glGetShaderInfoLog(vs, infoLen, NULL, infoLog); + printf("Error compiling shader:\n%s\n", infoLog); + } + } + + GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fs, 1, &fragmentShader, NULL); + glCompileShader(fs); + glGetShaderiv(fs, GL_COMPILE_STATUS, &ok); + if (!ok) { + printf("Shader compilation error with fragment\n"); + GLint infoLen = 0; + glGetShaderiv (vs, GL_INFO_LOG_LENGTH, &infoLen); + if (infoLen > 1) + { + char* infoLog = (char *)malloc(sizeof(char) * infoLen+1); + glGetShaderInfoLog(vs, infoLen, NULL, infoLog); + printf("Error compiling shader:\n%s\n", infoLog); + } + } + + GLuint program = glCreateProgram(); + + glAttachShader(program, vs); + glAttachShader(program, fs); + glLinkProgram(program); + glGetProgramiv(program, GL_LINK_STATUS, &ok); + assert(ok); + + glUseProgram(program); + + //GLint lightmapLocation = glGetUniformLocation(program, "lightmap"); + //assert(lightmapLocation >= 0); + //glUniform1i(lightmapLocation, 1); // sampler2D? Is it the texture unit? + + GLint diffusemapLocation = glGetUniformLocation(program, "diffusemap"); + assert(diffusemapLocation >= 0); + glUniform1i(diffusemapLocation, 0); + + //GLint texgenscrollLocation = glGetUniformLocation(program, "texgenscroll"); + //assert(texgenscrollLocation >= 0); + + //GLint colorparamsLocation = glGetUniformLocation(program, "colorparams"); + //assert(colorparamsLocation >= 0); + + //GLfloat texgenscrollData[] = { 0, 0, 0, 0 }; + //glUniform4fv(texgenscrollLocation, 1, texgenscrollData); + + //GLfloat colorparamsData[] = { 2, 2, 2, 1 }; + //glUniform4fv(colorparamsLocation, 1, colorparamsData); + + { + GLfloat data[16]; + glGetFloatv(GL_MODELVIEW_MATRIX, data); + printf("Modelview: "); + for (int i = 0; i < 16; i++) printf("%.3f, ", data[i]); + printf("\n"); + //memset(data, 0, 16*4); + GLint modelViewLocation = glGetUniformLocation(program, "u_modelView"); + assert(modelViewLocation >= 0); + glUniformMatrix4fv(modelViewLocation, 1, GL_FALSE, data); + } + { + GLfloat data[16]; + glGetFloatv(GL_PROJECTION_MATRIX, data); + printf("Projection: "); + for (int i = 0; i < 16; i++) printf("%.3f, ", data[i]); + printf("\n"); + //memset(data, 0, 16*4); + GLint projectionLocation = glGetUniformLocation(program, "u_projection"); + assert(projectionLocation >= 0); + glUniformMatrix4fv(projectionLocation, 1, GL_FALSE, data); + } + +/* + glBindAttribLocation(program, 0, "a_position"); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 32, (void*)0); + glEnableVertexAttribArray(0); + + glBindAttribLocation(program, 1, "v_texCoord0"); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 32, (void*)16); + glEnableVertexAttribArray(1); +*/ + + // stop recording in the VAO + + glBindVertexArray_(0); + + // unbind all the stuff the VAO would save for us, so this is a valid test + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + glDisableClientState(GL_VERTEX_ARRAY); + + // draw with VAO + + glBindVertexArray_(vao); + + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*)12); + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*) 0); + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*)24); + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*)36); + + // END + + SDL_GL_SwapBuffers(); + + verify(); + +#if !EMSCRIPTEN + SDL_Delay(1500); +#endif + + // SDL_Quit(); + + return 0; +} + diff --git a/tests/cubegeom_pre2_vao2.c b/tests/cubegeom_pre2_vao2.c new file mode 100644 index 00000000..e3ab4d62 --- /dev/null +++ b/tests/cubegeom_pre2_vao2.c @@ -0,0 +1,381 @@ +/* +THIS WORK, INCLUDING THE SOURCE CODE, DOCUMENTATION +AND RELATED MEDIA AND DATA, IS PLACED INTO THE PUBLIC DOMAIN. + +THE ORIGINAL AUTHOR IS KYLE FOLEY. + +THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY +OF ANY KIND, NOT EVEN THE IMPLIED WARRANTY OF +MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE, +ASSUMES _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE +RESULTING FROM THE USE, MODIFICATION, OR +REDISTRIBUTION OF THIS SOFTWARE. +*/ + +#if !EMSCRIPTEN +#define USE_GLEW 1 +#endif + +#if USE_GLEW +#include "GL/glew.h" +#endif + +#include "SDL/SDL.h" +#if !USE_GLEW +#include "SDL/SDL_opengl.h" +#endif + +#include <stdio.h> +#include <string.h> +#include <assert.h> + +PFNGLGENVERTEXARRAYSPROC glGenVertexArrays_ = NULL; +PFNGLBINDVERTEXARRAYPROC glBindVertexArray_ = NULL; +PFNGLDELETEVERTEXARRAYSPROC glDeleteVertexArrays_ = NULL; + +void verify() { + int width = 640, height = 480; + unsigned char *data = (unsigned char*)malloc(width*height*4); + glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data); + int sum = 0; + for (int x = 0; x < width*height*4; x++) { + if (x % 4 != 3) sum += x * data[x]; + } +#if EMSCRIPTEN + int result = sum; + REPORT_RESULT(); +#endif +} + +int main(int argc, char *argv[]) +{ + SDL_Surface *screen; + if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) { + printf("Unable to initialize SDL: %s\n", SDL_GetError()); + return 1; + } + + SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); + screen = SDL_SetVideoMode( 640, 480, 24, SDL_OPENGL ); + if ( !screen ) { + printf("Unable to set video mode: %s\n", SDL_GetError()); + return 1; + } + + glClearColor( 0, 0, 0, 0 ); + glClear( GL_COLOR_BUFFER_BIT ); + + glGenVertexArrays_ = (PFNGLGENVERTEXARRAYSPROC) SDL_GL_GetProcAddress("glGenVertexArrays"); + assert(glGenVertexArrays_); + glBindVertexArray_ = (PFNGLBINDVERTEXARRAYPROC) SDL_GL_GetProcAddress("glBindVertexArray"); + assert(glBindVertexArray_); + glDeleteVertexArrays_ = (PFNGLDELETEVERTEXARRAYSPROC) SDL_GL_GetProcAddress("glDeleteVertexArrays"); + assert(glDeleteVertexArrays_); + + glEnableClientState(GL_TEXTURE_COORD_ARRAY); // enabling it *before* the vao does nothing, the vao should wipe it out! + + // Generate a VAO + GLuint vao; + glGenVertexArrays_(1, &vao); + glBindVertexArray_(vao); + + // Create a texture + + GLuint texture; + glGenTextures( 1, &texture ); + glBindTexture( GL_TEXTURE_2D, texture ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); + GLubyte textureData[16*16*4]; + for (int x = 0; x < 16; x++) { + for (int y = 0; y < 16; y++) { + *((int*)&textureData[(x*16 + y) * 4]) = x*16 + ((y*16) << 8); + } + } + glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0, + GL_RGBA, GL_UNSIGNED_BYTE, textureData ); + + // Create a second texture + + GLuint texture2; + glGenTextures( 1, &texture2 ); + glBindTexture( GL_TEXTURE_2D, texture2 ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); + GLubyte texture2Data[] = { 0xff, 0, 0, 0xff, + 0, 0xff, 0, 0xaa, + 0, 0, 0xff, 0x55, + 0x80, 0x90, 0x70, 0 }; + glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, + GL_RGBA, GL_UNSIGNED_BYTE, texture2Data ); + + // BEGIN + +#if USE_GLEW + glewInit(); +#endif + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + // original: glFrustum(-0.6435469817188064, 0.6435469817188064 ,-0.48266022190470925, 0.48266022190470925 ,0.5400000214576721, 2048); + //glFrustum(-0.6435469817188064, 0.1435469817188064 ,-0.48266022190470925, 0.88266022190470925 ,0.5400000214576721, 2048); + GLfloat pm[] = { 1.372136116027832, 0, 0, 0, 0, 0.7910231351852417, 0, 0, -0.6352481842041016, 0.29297152161598206, -1.0005275011062622, -1, 0, 0, -1.080284833908081, 0 }; + glLoadMatrixf(pm); + + glMatrixMode(GL_MODELVIEW); + GLfloat matrixData[] = { -1, 0, 0, 0, + 0, 0,-1, 0, + 0, 1, 0, 0, + 0, 0, 0, 1 }; + glLoadMatrixf(matrixData); + //glTranslated(-512,-512,-527); // XXX this should be uncommented, but if it is then nothing is shown + +// glEnable(GL_CULL_FACE); + // glEnable(GL_DEPTH_TEST); + + //glClear(GL_DEPTH_BUFFER_BIT); + +// glEnableClientState(GL_NORMAL_ARRAY); + // glEnableClientState(GL_COLOR_ARRAY); + + glActiveTexture(GL_TEXTURE0); + + glEnableClientState(GL_VERTEX_ARRAY); + + GLuint arrayBuffer, elementBuffer; + glGenBuffers(1, &arrayBuffer); + glGenBuffers(1, &elementBuffer); + + GLubyte arrayData[] = { +/* +[0, 0, 0, 67] ==> 128 float +[0, 0, 128, 67] ==> 256 float +[0, 0, 0, 68] ==> 512 float +[0, 0, 128, 68] ==> 1024 float + +[vertex x ] [vertex y ] [vertex z ] [nr] [texture u ] [texture v ] [lm u ] [lm v ] [color r,g,b,a ] */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, // 0 + 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, // 1 + 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 2 + 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 3 + 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, // 4 + 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 67, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, // 5 + 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 67, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 6 + 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 7 + 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 8 + 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 9 + 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 128, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 10 + 0, 0, 0, 0, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 11 + 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 12 + 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 67, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 13 + 0, 0, 128, 68, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 67, 0, 0, 128, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 14 + 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 128, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 15 + + 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 0, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 128, 68, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128 + }; + assert(sizeof(arrayData) == 1408); + glBindBuffer(GL_ARRAY_BUFFER, arrayBuffer); + glBufferData(GL_ARRAY_BUFFER, sizeof(arrayData), arrayData, GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); + + GLushort elementData[] = { 1, 2, 0, 2, 3, 0, 5, 6, 4, 6, 7, 4, 9, 10, 8, 10, 11, 8, 13, 14, 12, 14, 15, 12 }; + assert(sizeof(elementData) == 48); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elementData), elementData, GL_STATIC_DRAW); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + + glBindBuffer(GL_ARRAY_BUFFER, arrayBuffer); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer); + + // sauer vertex data is apparently 0-12: V3F, 12: N1B, 16-24: T2F, 24-28: T2S, 28-32: C4B + glVertexPointer(3, GL_FLOAT, 32, (void*)0); // all these apply to the ARRAY_BUFFER that is bound + glTexCoordPointer(2, GL_FLOAT, 32, (void*)16); +// glClientActiveTexture(GL_TEXTURE1); // XXX seems to be ignored in native build +// glTexCoordPointer(2, GL_SHORT, 32, (void*)24); +// glClientActiveTexture(GL_TEXTURE0); // likely not needed, it is a cleanup +// glNormalPointer(GL_BYTE, 32, (void*)12); +// glColorPointer(4, GL_UNSIGNED_BYTE, 32, (void*)28); + + glBindTexture(GL_TEXTURE_2D, texture); // diffuse? + glActiveTexture(GL_TEXTURE0); + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, texture2); // lightmap? + glActiveTexture(GL_TEXTURE0); + + GLint ok; + + const char *vertexShader = "uniform mat4 u_modelView;\n" + "uniform mat4 u_projection;\n" + "varying vec4 v_texCoord0;\n" + "void main(void)\n" + "{\n" // (gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex) + // (u_projection * u_modelView * a_position) + " gl_Position = (u_projection * u_modelView * gl_Vertex) + vec4(200, 0, 0, 0);\n" + " v_texCoord0.xy = gl_MultiTexCoord0.xy/20.0;\n" // added /100 here + "}\n"; + const char *fragmentShader = "uniform sampler2D diffusemap;\n" + "varying vec4 v_texCoord0;\n" + "void main(void)\n" + "{\n" + " vec4 diffuse = texture2D(diffusemap, v_texCoord0.xy);\n" + " gl_FragColor = diffuse;\n" + "}\n"; + + GLuint vs = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vs, 1, &vertexShader, NULL); + glCompileShader(vs); + glGetShaderiv(vs, GL_COMPILE_STATUS, &ok); + if (!ok) { + printf("Shader compilation error with vertex\n"); + GLint infoLen = 0; + glGetShaderiv (vs, GL_INFO_LOG_LENGTH, &infoLen); + if (infoLen > 1) + { + char* infoLog = (char *)malloc(sizeof(char) * infoLen+1); + glGetShaderInfoLog(vs, infoLen, NULL, infoLog); + printf("Error compiling shader:\n%s\n", infoLog); + } + } + + GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fs, 1, &fragmentShader, NULL); + glCompileShader(fs); + glGetShaderiv(fs, GL_COMPILE_STATUS, &ok); + if (!ok) { + printf("Shader compilation error with fragment\n"); + GLint infoLen = 0; + glGetShaderiv (vs, GL_INFO_LOG_LENGTH, &infoLen); + if (infoLen > 1) + { + char* infoLog = (char *)malloc(sizeof(char) * infoLen+1); + glGetShaderInfoLog(vs, infoLen, NULL, infoLog); + printf("Error compiling shader:\n%s\n", infoLog); + } + } + + GLuint program = glCreateProgram(); + + glAttachShader(program, vs); + glAttachShader(program, fs); + glLinkProgram(program); + glGetProgramiv(program, GL_LINK_STATUS, &ok); + assert(ok); + + glUseProgram(program); + + //GLint lightmapLocation = glGetUniformLocation(program, "lightmap"); + //assert(lightmapLocation >= 0); + //glUniform1i(lightmapLocation, 1); // sampler2D? Is it the texture unit? + + GLint diffusemapLocation = glGetUniformLocation(program, "diffusemap"); + assert(diffusemapLocation >= 0); + glUniform1i(diffusemapLocation, 0); + + //GLint texgenscrollLocation = glGetUniformLocation(program, "texgenscroll"); + //assert(texgenscrollLocation >= 0); + + //GLint colorparamsLocation = glGetUniformLocation(program, "colorparams"); + //assert(colorparamsLocation >= 0); + + //GLfloat texgenscrollData[] = { 0, 0, 0, 0 }; + //glUniform4fv(texgenscrollLocation, 1, texgenscrollData); + + //GLfloat colorparamsData[] = { 2, 2, 2, 1 }; + //glUniform4fv(colorparamsLocation, 1, colorparamsData); + + { + GLfloat data[16]; + glGetFloatv(GL_MODELVIEW_MATRIX, data); + printf("Modelview: "); + for (int i = 0; i < 16; i++) printf("%.3f, ", data[i]); + printf("\n"); + //memset(data, 0, 16*4); + GLint modelViewLocation = glGetUniformLocation(program, "u_modelView"); + assert(modelViewLocation >= 0); + glUniformMatrix4fv(modelViewLocation, 1, GL_FALSE, data); + } + { + GLfloat data[16]; + glGetFloatv(GL_PROJECTION_MATRIX, data); + printf("Projection: "); + for (int i = 0; i < 16; i++) printf("%.3f, ", data[i]); + printf("\n"); + //memset(data, 0, 16*4); + GLint projectionLocation = glGetUniformLocation(program, "u_projection"); + assert(projectionLocation >= 0); + glUniformMatrix4fv(projectionLocation, 1, GL_FALSE, data); + } + +/* + glBindAttribLocation(program, 0, "a_position"); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 32, (void*)0); + glEnableVertexAttribArray(0); + + glBindAttribLocation(program, 1, "v_texCoord0"); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 32, (void*)16); + glEnableVertexAttribArray(1); +*/ + + // stop recording in the VAO + + glBindVertexArray_(0); + + // unbind all the stuff the VAO would save for us, so this is a valid test + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + glDisableClientState(GL_VERTEX_ARRAY); + + // draw with VAO + + glBindVertexArray_(vao); + + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*)12); + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*) 0); + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*)24); + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*)36); + + // END + + SDL_GL_SwapBuffers(); + + verify(); + +#if !EMSCRIPTEN + SDL_Delay(1500); +#endif + + // SDL_Quit(); + + return 0; +} + diff --git a/tests/cubegeom_pre_vao.c b/tests/cubegeom_pre_vao.c new file mode 100644 index 00000000..f1d35fb5 --- /dev/null +++ b/tests/cubegeom_pre_vao.c @@ -0,0 +1,333 @@ +/* +THIS WORK, INCLUDING THE SOURCE CODE, DOCUMENTATION +AND RELATED MEDIA AND DATA, IS PLACED INTO THE PUBLIC DOMAIN. + +THE ORIGINAL AUTHOR IS KYLE FOLEY. + +THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY +OF ANY KIND, NOT EVEN THE IMPLIED WARRANTY OF +MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE, +ASSUMES _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE +RESULTING FROM THE USE, MODIFICATION, OR +REDISTRIBUTION OF THIS SOFTWARE. +*/ + +#if !EMSCRIPTEN +#define USE_GLEW 1 +#endif + +#if USE_GLEW +#include "GL/glew.h" +#endif + +#include "SDL/SDL.h" +#if !USE_GLEW +#include "SDL/SDL_opengl.h" +#endif + +#include <stdio.h> +#include <string.h> +#include <assert.h> + +void verify() { + int width = 640, height = 480; + unsigned char *data = (unsigned char*)malloc(width*height*4); + glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data); + int sum = 0; + for (int x = 0; x < width*height*4; x++) { + if (x % 4 != 3) sum += x * data[x]; + } +#if EMSCRIPTEN + int result = sum; + REPORT_RESULT(); +#endif +} + +int main(int argc, char *argv[]) +{ + SDL_Surface *screen; + if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) { + printf("Unable to initialize SDL: %s\n", SDL_GetError()); + return 1; + } + + SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); + screen = SDL_SetVideoMode( 640, 480, 24, SDL_OPENGL ); + if ( !screen ) { + printf("Unable to set video mode: %s\n", SDL_GetError()); + return 1; + } + + glClearColor( 0, 0, 0, 0 ); + glClear( GL_COLOR_BUFFER_BIT ); + + // Create a texture + + GLuint texture; + glGenTextures( 1, &texture ); + glBindTexture( GL_TEXTURE_2D, texture ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); + GLubyte textureData[16*16*4]; + for (int x = 0; x < 16; x++) { + for (int y = 0; y < 16; y++) { + *((int*)&textureData[(x*16 + y) * 4]) = x*16 + ((y*16) << 8); + } + } + glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0, + GL_RGBA, GL_UNSIGNED_BYTE, textureData ); + + // Create a second texture + + GLuint texture2; + glGenTextures( 1, &texture2 ); + glBindTexture( GL_TEXTURE_2D, texture2 ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); + GLubyte texture2Data[] = { 0xff, 0, 0, 0xff, + 0, 0xff, 0, 0xaa, + 0, 0, 0xff, 0x55, + 0x80, 0x90, 0x70, 0 }; + glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, + GL_RGBA, GL_UNSIGNED_BYTE, texture2Data ); + + // BEGIN + +#if USE_GLEW + glewInit(); +#endif + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + // original: glFrustum(-0.6435469817188064, 0.6435469817188064 ,-0.48266022190470925, 0.48266022190470925 ,0.5400000214576721, 2048); + //glFrustum(-0.6435469817188064, 0.1435469817188064 ,-0.48266022190470925, 0.88266022190470925 ,0.5400000214576721, 2048); + GLfloat pm[] = { 1.372136116027832, 0, 0, 0, 0, 0.7910231351852417, 0, 0, -0.6352481842041016, 0.29297152161598206, -1.0005275011062622, -1, 0, 0, -1.080284833908081, 0 }; + glLoadMatrixf(pm); + + glMatrixMode(GL_MODELVIEW); + GLfloat matrixData[] = { -1, 0, 0, 0, + 0, 0,-1, 0, + 0, 1, 0, 0, + 0, 0, 0, 1 }; + glLoadMatrixf(matrixData); + + glActiveTexture(GL_TEXTURE0); + + GLuint arrayBuffer, elementBuffer; + glGenBuffers(1, &arrayBuffer); + glGenBuffers(1, &elementBuffer); + + GLubyte arrayData[] = { +/* +[0, 0, 0, 67] ==> 128 float +[0, 0, 128, 67] ==> 256 float +[0, 0, 0, 68] ==> 512 float +[0, 0, 128, 68] ==> 1024 float + +[vertex x ] [vertex y ] [vertex z ] [nr] [texture u ] [texture v ] [lm u ] [lm v ] [color r,g,b,a ] */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, // 0 + 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, // 1 + 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 2 + 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 3 + 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, // 4 + 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 67, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, // 5 + 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 67, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 6 + 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 7 + 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 8 + 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 9 + 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 128, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 10 + 0, 0, 0, 0, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 11 + 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 12 + 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 67, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 13 + 0, 0, 128, 68, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 67, 0, 0, 128, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 14 + 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 128, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 15 + + 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 0, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, + 0, 0, 128, 68, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128 + }; + + // Generate a VAO + GLuint vao; + glGenVertexArrays(1, &vao); + glBindVertexArray(vao); + + assert(sizeof(arrayData) == 1408); + glBindBuffer(GL_ARRAY_BUFFER, arrayBuffer); + glBufferData(GL_ARRAY_BUFFER, sizeof(arrayData), arrayData, GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); + + GLushort elementData[] = { 1, 2, 0, 2, 3, 0, 5, 6, 4, 6, 7, 4, 9, 10, 8, 10, 11, 8, 13, 14, 12, 14, 15, 12 }; + assert(sizeof(elementData) == 48); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elementData), elementData, GL_STATIC_DRAW); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + + glBindBuffer(GL_ARRAY_BUFFER, arrayBuffer); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer); + + glBindTexture(GL_TEXTURE_2D, texture); // diffuse? + glActiveTexture(GL_TEXTURE0); + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, texture2); // lightmap? + glActiveTexture(GL_TEXTURE0); + + GLint ok; + + const char *vertexShader = "attribute vec4 a_position;\n" + "attribute vec4 a_texCoord0;\n" + "uniform mat4 u_modelView;\n" + "uniform mat4 u_projection;\n" + "varying vec4 v_texCoord0;\n" + "void main(void)\n" + "{\n" + " gl_Position = (u_projection * u_modelView * a_position) + vec4(200, 0, 0, 0);\n" + " v_texCoord0.xy = a_texCoord0.xy/20.0;\n" // added /20 here + "}\n"; + const char *fragmentShader = "uniform sampler2D diffusemap;\n" + "varying vec4 v_texCoord0;\n" + "void main(void)\n" + "{\n" + " vec4 diffuse = texture2D(diffusemap, v_texCoord0.xy);\n" + " gl_FragColor = diffuse;\n" + "}\n"; + + GLuint vs = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vs, 1, &vertexShader, NULL); + glCompileShader(vs); + glGetShaderiv(vs, GL_COMPILE_STATUS, &ok); + if (!ok) { + printf("Shader compilation error with vertex\n"); + GLint infoLen = 0; + glGetShaderiv (vs, GL_INFO_LOG_LENGTH, &infoLen); + if (infoLen > 1) + { + char* infoLog = (char *)malloc(sizeof(char) * infoLen+1); + glGetShaderInfoLog(vs, infoLen, NULL, infoLog); + printf("Error compiling shader:\n%s\n", infoLog); + } + } + + GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fs, 1, &fragmentShader, NULL); + glCompileShader(fs); + glGetShaderiv(fs, GL_COMPILE_STATUS, &ok); + if (!ok) { + printf("Shader compilation error with fragment\n"); + GLint infoLen = 0; + glGetShaderiv (vs, GL_INFO_LOG_LENGTH, &infoLen); + if (infoLen > 1) + { + char* infoLog = (char *)malloc(sizeof(char) * infoLen+1); + glGetShaderInfoLog(vs, infoLen, NULL, infoLog); + printf("Error compiling shader:\n%s\n", infoLog); + } + } + + GLuint program = glCreateProgram(); + + glAttachShader(program, vs); + glAttachShader(program, fs); + glLinkProgram(program); + glGetProgramiv(program, GL_LINK_STATUS, &ok); + assert(ok); + + glUseProgram(program); + + GLint diffusemapLocation = glGetUniformLocation(program, "diffusemap"); + assert(diffusemapLocation >= 0); + glUniform1i(diffusemapLocation, 0); + + { + GLfloat data[16]; + glGetFloatv(GL_MODELVIEW_MATRIX, data); + printf("Modelview: "); + for (int i = 0; i < 16; i++) printf("%.3f, ", data[i]); + printf("\n"); + GLint modelViewLocation = glGetUniformLocation(program, "u_modelView"); + assert(modelViewLocation >= 0); + glUniformMatrix4fv(modelViewLocation, 1, GL_FALSE, data); + } + { + GLfloat data[16]; + glGetFloatv(GL_PROJECTION_MATRIX, data); + printf("Projection: "); + for (int i = 0; i < 16; i++) printf("%.3f, ", data[i]); + printf("\n"); + GLint projectionLocation = glGetUniformLocation(program, "u_projection"); + assert(projectionLocation >= 0); + glUniformMatrix4fv(projectionLocation, 1, GL_FALSE, data); + } + + glBindAttribLocation(program, 0, "a_position"); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 32, (void*)0); + glEnableVertexAttribArray(0); + + glBindAttribLocation(program, 1, "a_texCoord0"); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 32, (void*)16); + glEnableVertexAttribArray(1); + + // stop recording in the VAO + + glBindVertexArray(0); + + // unbind all the stuff the VAO would save for us, so this is a valid test + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + + glDisableVertexAttribArray(0); + glDisableVertexAttribArray(1); + + // draw with VAO + + glBindVertexArray(vao); + + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*)12); + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*) 0); + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*)24); + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*)36); + + glBindVertexArray(0); + + glDeleteVertexArrays(1, &vao); + + // END + + SDL_GL_SwapBuffers(); + + verify(); + +#if !EMSCRIPTEN + SDL_Delay(1500); +#endif + + SDL_Quit(); + + return 0; +} diff --git a/tests/dlmalloc_test.c b/tests/dlmalloc_test.c index 817778bd..fc640d46 100644 --- a/tests/dlmalloc_test.c +++ b/tests/dlmalloc_test.c @@ -42,8 +42,12 @@ int main(int ac, char **av) //printf("zz last: %d\n", (int)last); char *newer = (char*)malloc(512); // should be different //printf("zz newer: %d\n", (int)newer); +#ifndef __APPLE__ c1 += first == last; c2 += first == newer; +#else // On OSX, it's been detected that memory is not necessarily allocated linearly, so skip this check and simulate success. + ++c1; +#endif } printf("*%d,%d*\n", c1, c2); } diff --git a/tests/float_tex.png b/tests/float_tex.png Binary files differindex 8c3c6502..8fc005ff 100644 --- a/tests/float_tex.png +++ b/tests/float_tex.png diff --git a/tests/fuzz/1.c b/tests/fuzz/1.c new file mode 100644 index 00000000..2017fb76 --- /dev/null +++ b/tests/fuzz/1.c @@ -0,0 +1,117 @@ +/* + * This is a RANDOMLY GENERATED PROGRAM. + * + * Generator: csmith 2.2.0 + * Git version: a8697aa + * Options: --no-volatiles --no-math64 --max-block-depth 2 --max-block-size 2 --max-expr-complexity 2 --max-funcs 2 + * Seed: 1880513882 + */ + +#include "csmith.h" + + +static long __undefined; + +/* --- Struct/Union Declarations --- */ +union U0 { + int32_t f0; + int8_t f1; + const int8_t f2; +}; + +/* --- GLOBAL VARIABLES --- */ +static uint32_t g_4 = 9UL; +static int32_t g_6 = 0xB9DD952EL; +static const int32_t *g_5 = &g_6; +static union U0 g_7[3][9][6] = {{{{-8L},{5L},{0x901516EAL},{7L},{7L},{0x901516EAL}},{{0x520EA0C8L},{0x520EA0C8L},{0x0920A6FFL},{0x0021FBB9L},{0x888C5540L},{-8L}},{{0xDC87A9B6L},{0x42B48371L},{8L},{7L},{-8L},{0x0920A6FFL}},{{-1L},{0xDC87A9B6L},{8L},{5L},{0x520EA0C8L},{-8L}},{{-3L},{5L},{0x0920A6FFL},{0x865B49D5L},{0xDC87A9B6L},{0x901516EAL}},{{0x865B49D5L},{0xDC87A9B6L},{0x901516EAL},{0x0021FBB9L},{-1L},{-1L}},{{0x865B49D5L},{0x42B48371L},{0x42B48371L},{0x865B49D5L},{-3L},{0x0920A6FFL}},{{-3L},{0x520EA0C8L},{0x0021FBB9L},{5L},{0x865B49D5L},{-1L}},{{-1L},{5L},{0L},{7L},{0x865B49D5L},{0x901516EAL}}},{{{0xDC87A9B6L},{0x520EA0C8L},{7L},{0x0021FBB9L},{-3L},{-8L}},{{0x520EA0C8L},{0x42B48371L},{5L},{7L},{-1L},{0x0920A6FFL}},{{-8L},{0xDC87A9B6L},{5L},{5L},{0xDC87A9B6L},{-8L}},{{0x888C5540L},{5L},{7L},{0x865B49D5L},{0x520EA0C8L},{0x901516EAL}},{{7L},{0xDC87A9B6L},{0L},{0x0021FBB9L},{-8L},{-1L}},{{7L},{0x42B48371L},{0x0021FBB9L},{0x865B49D5L},{0x888C5540L},{0x0920A6FFL}},{{0x888C5540L},{0x520EA0C8L},{0x42B48371L},{5L},{7L},{-1L}},{{-8L},{5L},{0x901516EAL},{7L},{7L},{0x901516EAL}},{{0x520EA0C8L},{0x520EA0C8L},{0x0920A6FFL},{0x0021FBB9L},{0x888C5540L},{-8L}}},{{{0xDC87A9B6L},{0x42B48371L},{8L},{7L},{-8L},{0x0920A6FFL}},{{-1L},{0xDC87A9B6L},{8L},{5L},{0x520EA0C8L},{-8L}},{{-3L},{5L},{0x0920A6FFL},{0x865B49D5L},{0xDC87A9B6L},{0x901516EAL}},{{0x865B49D5L},{0xDC87A9B6L},{0x901516EAL},{0x0021FBB9L},{-1L},{-1L}},{{0x865B49D5L},{0x42B48371L},{0x42B48371L},{0x865B49D5L},{-3L},{0x0920A6FFL}},{{-3L},{0x520EA0C8L},{0x0021FBB9L},{5L},{0x865B49D5L},{-1L}},{{-1L},{5L},{0L},{7L},{0x865B49D5L},{0x901516EAL}},{{0xDC87A9B6L},{0x520EA0C8L},{7L},{0x0021FBB9L},{-3L},{-8L}},{{0x520EA0C8L},{0x42B48371L},{5L},{7L},{-1L},{0x0920A6FFL}}}}; +static int32_t g_31 = 1L; +static uint32_t g_32 = 4294967289UL; + + +/* --- FORWARD DECLARATIONS --- */ +static uint16_t func_1(void); +static int32_t * func_9(int32_t * p_10, uint16_t p_11); + + +/* --- FUNCTIONS --- */ +/* ------------------------------------------ */ +/* + * reads : g_4 g_5 g_7 g_32 + * writes: g_5 g_32 + */ +static uint16_t func_1(void) +{ /* block id: 0 */ + int32_t l_3 = (-1L); + if (((safe_unary_minus_func_int16_t_s(l_3)) >= g_4)) + { /* block id: 1 */ + int32_t *l_8 = &l_3; + g_5 = g_5; + (*l_8) ^= (g_7[1][3][2] , 0x44688D23L); + } + else + { /* block id: 4 */ + int32_t *l_12 = &l_3; + int32_t **l_13 = &l_12; + (*l_13) = func_9(((*l_13) = l_12), l_3); + } + return l_3; +} + + +/* ------------------------------------------ */ +/* + * reads : g_32 + * writes: g_32 + */ +static int32_t * func_9(int32_t * p_10, uint16_t p_11) +{ /* block id: 6 */ + int32_t *l_14 = &g_6; + int32_t *l_15 = &g_7[1][3][2].f0; + int32_t *l_16 = &g_6; + int32_t *l_17 = &g_6; + int32_t *l_18 = &g_6; + int32_t *l_19 = &g_6; + int32_t *l_20 = &g_6; + int32_t *l_21 = &g_6; + int32_t *l_22 = &g_6; + int32_t *l_23 = &g_7[1][3][2].f0; + int32_t l_24[8] = {0xF9F11119L,0xF9F11119L,0xF9F11119L,0xF9F11119L,0xF9F11119L,0xF9F11119L,0xF9F11119L,0xF9F11119L}; + int32_t *l_25 = &l_24[7]; + int32_t *l_26 = &l_24[2]; + int32_t *l_27 = &l_24[2]; + int32_t *l_28 = &l_24[0]; + int32_t *l_29 = &g_7[1][3][2].f0; + int32_t *l_30[10] = {(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0}; + int i; + ++g_32; + return p_10; +} + + + + +/* ---------------------------------------- */ +int main (int argc, char* argv[]) +{ + int i, j, k; + int print_hash_value = 0; + if (argc == 2 && strcmp(argv[1], "1") == 0) print_hash_value = 1; + platform_main_begin(); + crc32_gentab(); + func_1(); + for (i = 0; i < 3; i++) + { + for (j = 0; j < 1; j++) + { + for (k = 0; k < 1; k++) + { + transparent_crc(g_7[i][j][k].f0, "g_7[i][j][k].f0", print_hash_value); + transparent_crc(g_7[i][j][k].f1, "g_7[i][j][k].f1", print_hash_value); + if (print_hash_value) printf("index = [%d][%d][%d]\n", i, j, k); + } + } + } + platform_main_end(crc32_context ^ 0xFFFFFFFFUL, print_hash_value); + return 0; +} + diff --git a/tests/fuzz/1.c.txt b/tests/fuzz/1.c.txt new file mode 100644 index 00000000..701369f3 --- /dev/null +++ b/tests/fuzz/1.c.txt @@ -0,0 +1 @@ +checksum = 504CCDC diff --git a/tests/fuzz/2.c b/tests/fuzz/2.c new file mode 100644 index 00000000..2063b2d1 --- /dev/null +++ b/tests/fuzz/2.c @@ -0,0 +1,2006 @@ +/* + * This is a RANDOMLY GENERATED PROGRAM. + * + * Generator: csmith 2.2.0 + * Git version: 2751ded + * Options: --no-volatiles --no-math64 + * Seed: 2601498443 + */ + +#include "csmith.h" + + +static long __undefined; + +/* --- Struct/Union Declarations --- */ +#pragma pack(push) +#pragma pack(1) +struct S0 { + uint32_t f0; + int16_t f1; + const uint16_t f2; + uint16_t f3; + uint8_t f4; +}; +#pragma pack(pop) + +struct S1 { + unsigned f0 : 4; + signed f1 : 23; + unsigned f2 : 21; + const unsigned f3 : 16; + unsigned f4 : 22; + signed f5 : 11; + signed f6 : 25; + unsigned f7 : 15; + unsigned f8 : 13; + uint32_t f9; +}; + +/* --- GLOBAL VARIABLES --- */ +static int32_t g_2 = 0xBD6C3F0BL; +static uint8_t g_5 = 0xC4L; +static struct S0 g_18 = {18446744073709551615UL,5L,0x0498L,0x20B5L,0xF6L}; +static int8_t g_36[8] = {0x09L,0x65L,0x09L,0x09L,0x65L,0x09L,0x09L,0x65L}; +static int32_t g_39 = 5L; +static int8_t g_40 = 9L; +static uint16_t g_71 = 0x2B10L; +static uint16_t g_79 = 0UL; +static struct S0 g_82 = {0xF60EFD82L,2L,0x24F1L,65530UL,7UL}; +static uint8_t g_119 = 4UL; +static uint32_t g_195 = 0x6554C541L; +static int32_t *g_199 = (void*)0; +static int16_t g_208 = 0L; +static uint32_t g_209 = 0UL; +static int32_t **g_220 = &g_199; +static uint16_t g_222 = 9UL; +static int32_t g_237 = 0x16D0E17FL; +static int32_t g_238 = (-6L); +static int32_t g_240 = 5L; +static uint32_t g_241 = 0x4C8054E4L; +static uint32_t g_251 = 0x944D6970L; +static uint8_t **g_254 = (void*)0; +static int8_t g_279 = 0x60L; +static int8_t g_280 = 1L; +static int32_t g_281 = 0x4E1FA7EEL; +static uint16_t g_282 = 1UL; +static int16_t *g_313[9][9][3] = {{{&g_208,&g_82.f1,(void*)0},{&g_82.f1,(void*)0,&g_208},{&g_208,&g_18.f1,(void*)0},{&g_82.f1,(void*)0,(void*)0},{&g_18.f1,&g_208,&g_82.f1},{&g_82.f1,&g_82.f1,&g_18.f1},{&g_208,&g_208,&g_18.f1},{&g_82.f1,&g_208,(void*)0},{&g_208,(void*)0,&g_18.f1}},{{&g_82.f1,&g_208,&g_18.f1},{&g_82.f1,&g_208,&g_208},{(void*)0,(void*)0,&g_82.f1},{&g_208,&g_208,&g_208},{&g_208,&g_18.f1,&g_208},{(void*)0,&g_208,&g_208},{(void*)0,(void*)0,&g_208},{&g_82.f1,(void*)0,&g_208},{&g_208,(void*)0,&g_208}},{{&g_18.f1,&g_208,&g_208},{&g_82.f1,&g_208,&g_18.f1},{&g_208,&g_18.f1,&g_82.f1},{&g_18.f1,&g_18.f1,&g_208},{&g_18.f1,&g_18.f1,&g_18.f1},{&g_208,(void*)0,(void*)0},{&g_82.f1,&g_18.f1,&g_82.f1},{&g_18.f1,(void*)0,&g_82.f1},{&g_208,&g_18.f1,&g_18.f1}},{{&g_82.f1,&g_18.f1,&g_208},{(void*)0,&g_18.f1,(void*)0},{(void*)0,(void*)0,&g_82.f1},{&g_208,&g_18.f1,(void*)0},{&g_208,(void*)0,&g_82.f1},{&g_18.f1,&g_18.f1,&g_208},{(void*)0,&g_18.f1,&g_208},{(void*)0,&g_18.f1,&g_82.f1},{(void*)0,&g_208,(void*)0}},{{&g_18.f1,&g_208,&g_82.f1},{&g_82.f1,(void*)0,(void*)0},{(void*)0,(void*)0,&g_208},{&g_82.f1,(void*)0,&g_18.f1},{(void*)0,&g_208,&g_82.f1},{&g_82.f1,&g_18.f1,&g_82.f1},{&g_18.f1,(void*)0,(void*)0},{(void*)0,&g_208,&g_18.f1},{(void*)0,&g_208,&g_18.f1}},{{&g_18.f1,&g_208,&g_18.f1},{(void*)0,&g_82.f1,&g_18.f1},{&g_208,&g_82.f1,&g_82.f1},{&g_18.f1,&g_208,(void*)0},{&g_82.f1,&g_208,&g_18.f1},{&g_208,&g_208,&g_208},{(void*)0,&g_82.f1,&g_18.f1},{&g_208,&g_208,(void*)0},{&g_18.f1,&g_82.f1,&g_82.f1}},{{&g_18.f1,&g_208,&g_18.f1},{&g_18.f1,(void*)0,&g_18.f1},{&g_208,&g_18.f1,&g_18.f1},{&g_208,&g_18.f1,&g_18.f1},{&g_18.f1,&g_82.f1,&g_82.f1},{&g_18.f1,&g_208,&g_18.f1},{&g_18.f1,&g_208,(void*)0},{&g_208,&g_18.f1,&g_18.f1},{(void*)0,(void*)0,(void*)0}},{{&g_208,&g_18.f1,&g_82.f1},{&g_82.f1,&g_208,&g_82.f1},{&g_18.f1,&g_208,(void*)0},{&g_208,&g_82.f1,&g_18.f1},{(void*)0,&g_18.f1,&g_82.f1},{&g_18.f1,&g_18.f1,&g_82.f1},{(void*)0,(void*)0,&g_18.f1},{&g_208,&g_208,(void*)0},{&g_208,&g_82.f1,&g_82.f1}},{{(void*)0,&g_208,&g_82.f1},{&g_18.f1,&g_82.f1,(void*)0},{&g_208,&g_208,&g_18.f1},{&g_18.f1,&g_208,(void*)0},{(void*)0,&g_82.f1,&g_208},{&g_18.f1,&g_82.f1,&g_208},{(void*)0,&g_82.f1,(void*)0},{(void*)0,&g_208,&g_82.f1},{&g_82.f1,&g_208,&g_82.f1}}}; +static uint32_t g_347 = 0xC4FCF803L; +static int32_t *g_366 = &g_240; +static int32_t **g_365 = &g_366; +static struct S1 g_391 = {1,2228,1279,15,579,-29,789,104,13,4294967287UL}; +static struct S1 *g_390 = &g_391; +static uint32_t g_408 = 0xCF847D91L; +static int32_t g_433 = 0x55EDE5F7L; +static int32_t g_434 = 0xB4C5C7AAL; +static int32_t g_499[3][1] = {{0x2CC689D6L},{0x2CC689D6L},{0x2CC689D6L}}; +static int32_t g_500 = 8L; +static uint32_t g_501 = 7UL; +static struct S0 g_508[9][4][7] = {{{{0x66A29370L,1L,65533UL,0x58A9L,0xE0L},{0x66A29370L,1L,65533UL,0x58A9L,0xE0L},{0x2EB9A52EL,0x9EFDL,0x49E1L,0xB224L,0x6FL},{0x1A87F5EEL,0xD3E2L,0xED27L,65535UL,255UL},{0xDD780AAAL,0L,65532UL,0x908CL,0xFDL},{0x1A87F5EEL,0xD3E2L,0xED27L,65535UL,255UL},{0x2EB9A52EL,0x9EFDL,0x49E1L,0xB224L,0x6FL}},{{0xD58F413CL,0xF25BL,65531UL,0xB870L,0UL},{0xD58F413CL,0xF25BL,65531UL,0xB870L,0UL},{18446744073709551615UL,1L,0x9E3EL,0xDB7DL,1UL},{0x1A9DC0E9L,0x3641L,65527UL,0xD3FCL,1UL},{18446744073709551612UL,-1L,1UL,0x787BL,0UL},{0x1A9DC0E9L,0x3641L,65527UL,0xD3FCL,1UL},{18446744073709551615UL,1L,0x9E3EL,0xDB7DL,1UL}},{{0x66A29370L,1L,65533UL,0x58A9L,0xE0L},{0x66A29370L,1L,65533UL,0x58A9L,0xE0L},{0x2EB9A52EL,0x9EFDL,0x49E1L,0xB224L,0x6FL},{0x1A87F5EEL,0xD3E2L,0xED27L,65535UL,255UL},{0xDD780AAAL,0L,65532UL,0x908CL,0xFDL},{0x1A87F5EEL,0xD3E2L,0xED27L,65535UL,255UL},{0x2EB9A52EL,0x9EFDL,0x49E1L,0xB224L,0x6FL}},{{0xD58F413CL,0xF25BL,65531UL,0xB870L,0UL},{0xD58F413CL,0xF25BL,65531UL,0xB870L,0UL},{18446744073709551615UL,1L,0x9E3EL,0xDB7DL,1UL},{0x1A9DC0E9L,0x3641L,65527UL,0xD3FCL,1UL},{18446744073709551612UL,-1L,1UL,0x787BL,0UL},{0x1A9DC0E9L,0x3641L,65527UL,0xD3FCL,1UL},{18446744073709551615UL,1L,0x9E3EL,0xDB7DL,1UL}}},{{{0x66A29370L,1L,65533UL,0x58A9L,0xE0L},{0x66A29370L,1L,65533UL,0x58A9L,0xE0L},{0x2EB9A52EL,0x9EFDL,0x49E1L,0xB224L,0x6FL},{0x1A87F5EEL,0xD3E2L,0xED27L,65535UL,255UL},{0xDD780AAAL,0L,65532UL,0x908CL,0xFDL},{0x1A87F5EEL,0xD3E2L,0xED27L,65535UL,255UL},{0x2EB9A52EL,0x9EFDL,0x49E1L,0xB224L,0x6FL}},{{0xD58F413CL,0xF25BL,65531UL,0xB870L,0UL},{0xD58F413CL,0xF25BL,65531UL,0xB870L,0UL},{18446744073709551615UL,1L,0x9E3EL,0xDB7DL,1UL},{0x1A9DC0E9L,0x3641L,65527UL,0xD3FCL,1UL},{18446744073709551612UL,-1L,1UL,0x787BL,0UL},{0x1A9DC0E9L,0x3641L,65527UL,0xD3FCL,1UL},{18446744073709551615UL,1L,0x9E3EL,0xDB7DL,1UL}},{{0x66A29370L,1L,65533UL,0x58A9L,0xE0L},{0x66A29370L,1L,65533UL,0x58A9L,0xE0L},{0x2EB9A52EL,0x9EFDL,0x49E1L,0xB224L,0x6FL},{0x1A87F5EEL,0xD3E2L,0xED27L,65535UL,255UL},{0xDD780AAAL,0L,65532UL,0x908CL,0xFDL},{0x1A87F5EEL,0xD3E2L,0xED27L,65535UL,255UL},{0x2EB9A52EL,0x9EFDL,0x49E1L,0xB224L,0x6FL}},{{0xD58F413CL,0xF25BL,65531UL,0xB870L,0UL},{0xD58F413CL,0xF25BL,65531UL,0xB870L,0UL},{18446744073709551615UL,1L,0x9E3EL,0xDB7DL,1UL},{0x1A9DC0E9L,0x3641L,65527UL,0xD3FCL,1UL},{18446744073709551612UL,-1L,1UL,0x787BL,0UL},{0x1A9DC0E9L,0x3641L,65527UL,0xD3FCL,1UL},{18446744073709551615UL,1L,0x9E3EL,0xDB7DL,1UL}}},{{{0x66A29370L,1L,65533UL,0x58A9L,0xE0L},{0x66A29370L,1L,65533UL,0x58A9L,0xE0L},{0x2EB9A52EL,0x9EFDL,0x49E1L,0xB224L,0x6FL},{0x1A87F5EEL,0xD3E2L,0xED27L,65535UL,255UL},{0xDD780AAAL,0L,65532UL,0x908CL,0xFDL},{0x1A87F5EEL,0xD3E2L,0xED27L,65535UL,255UL},{0x2EB9A52EL,0x9EFDL,0x49E1L,0xB224L,0x6FL}},{{0xD58F413CL,0xF25BL,65531UL,0xB870L,0UL},{0xD58F413CL,0xF25BL,65531UL,0xB870L,0UL},{18446744073709551615UL,1L,0x9E3EL,0xDB7DL,1UL},{0x1A9DC0E9L,0x3641L,65527UL,0xD3FCL,1UL},{18446744073709551612UL,-1L,1UL,0x787BL,0UL},{0x1A9DC0E9L,0x3641L,65527UL,0xD3FCL,1UL},{18446744073709551615UL,1L,0x9E3EL,0xDB7DL,1UL}},{{0x66A29370L,1L,65533UL,0x58A9L,0xE0L},{0x66A29370L,1L,65533UL,0x58A9L,0xE0L},{0x2EB9A52EL,0x9EFDL,0x49E1L,0xB224L,0x6FL},{0x1A87F5EEL,0xD3E2L,0xED27L,65535UL,255UL},{0xDD780AAAL,0L,65532UL,0x908CL,0xFDL},{0x1A87F5EEL,0xD3E2L,0xED27L,65535UL,255UL},{0x2EB9A52EL,0x9EFDL,0x49E1L,0xB224L,0x6FL}},{{0xD58F413CL,0xF25BL,65531UL,0xB870L,0UL},{18446744073709551615UL,1L,0x9E3EL,0xDB7DL,1UL},{6UL,-1L,7UL,3UL,0xB7L},{18446744073709551615UL,0x582EL,8UL,0x124DL,1UL},{0xD58F413CL,0xF25BL,65531UL,0xB870L,0UL},{18446744073709551615UL,0x582EL,8UL,0x124DL,1UL},{6UL,-1L,7UL,3UL,0xB7L}}},{{{0x2EB9A52EL,0x9EFDL,0x49E1L,0xB224L,0x6FL},{0x2EB9A52EL,0x9EFDL,0x49E1L,0xB224L,0x6FL},{0xCB5D2F36L,5L,0xAAE3L,4UL,0x1FL},{1UL,-9L,0x4EAEL,0x67B1L,0x62L},{0x66A29370L,1L,65533UL,0x58A9L,0xE0L},{1UL,-9L,0x4EAEL,0x67B1L,0x62L},{0xCB5D2F36L,5L,0xAAE3L,4UL,0x1FL}},{{18446744073709551615UL,1L,0x9E3EL,0xDB7DL,1UL},{18446744073709551615UL,1L,0x9E3EL,0xDB7DL,1UL},{6UL,-1L,7UL,3UL,0xB7L},{18446744073709551615UL,0x582EL,8UL,0x124DL,1UL},{0xD58F413CL,0xF25BL,65531UL,0xB870L,0UL},{18446744073709551615UL,0x582EL,8UL,0x124DL,1UL},{6UL,-1L,7UL,3UL,0xB7L}},{{0x2EB9A52EL,0x9EFDL,0x49E1L,0xB224L,0x6FL},{0x2EB9A52EL,0x9EFDL,0x49E1L,0xB224L,0x6FL},{0xCB5D2F36L,5L,0xAAE3L,4UL,0x1FL},{1UL,-9L,0x4EAEL,0x67B1L,0x62L},{0x66A29370L,1L,65533UL,0x58A9L,0xE0L},{1UL,-9L,0x4EAEL,0x67B1L,0x62L},{0xCB5D2F36L,5L,0xAAE3L,4UL,0x1FL}},{{18446744073709551615UL,1L,0x9E3EL,0xDB7DL,1UL},{18446744073709551615UL,1L,0x9E3EL,0xDB7DL,1UL},{6UL,-1L,7UL,3UL,0xB7L},{18446744073709551615UL,0x582EL,8UL,0x124DL,1UL},{0xD58F413CL,0xF25BL,65531UL,0xB870L,0UL},{18446744073709551615UL,0x582EL,8UL,0x124DL,1UL},{6UL,-1L,7UL,3UL,0xB7L}}},{{{0x2EB9A52EL,0x9EFDL,0x49E1L,0xB224L,0x6FL},{0x2EB9A52EL,0x9EFDL,0x49E1L,0xB224L,0x6FL},{0xCB5D2F36L,5L,0xAAE3L,4UL,0x1FL},{1UL,-9L,0x4EAEL,0x67B1L,0x62L},{0x66A29370L,1L,65533UL,0x58A9L,0xE0L},{1UL,-9L,0x4EAEL,0x67B1L,0x62L},{0xCB5D2F36L,5L,0xAAE3L,4UL,0x1FL}},{{18446744073709551615UL,1L,0x9E3EL,0xDB7DL,1UL},{18446744073709551615UL,1L,0x9E3EL,0xDB7DL,1UL},{6UL,-1L,7UL,3UL,0xB7L},{18446744073709551615UL,0x582EL,8UL,0x124DL,1UL},{0xD58F413CL,0xF25BL,65531UL,0xB870L,0UL},{18446744073709551615UL,0x582EL,8UL,0x124DL,1UL},{6UL,-1L,7UL,3UL,0xB7L}},{{0x2EB9A52EL,0x9EFDL,0x49E1L,0xB224L,0x6FL},{0x2EB9A52EL,0x9EFDL,0x49E1L,0xB224L,0x6FL},{0xCB5D2F36L,5L,0xAAE3L,4UL,0x1FL},{1UL,-9L,0x4EAEL,0x67B1L,0x62L},{0x66A29370L,1L,65533UL,0x58A9L,0xE0L},{1UL,-9L,0x4EAEL,0x67B1L,0x62L},{0xCB5D2F36L,5L,0xAAE3L,4UL,0x1FL}},{{18446744073709551615UL,1L,0x9E3EL,0xDB7DL,1UL},{18446744073709551615UL,1L,0x9E3EL,0xDB7DL,1UL},{6UL,-1L,7UL,3UL,0xB7L},{18446744073709551615UL,0x582EL,8UL,0x124DL,1UL},{0xD58F413CL,0xF25BL,65531UL,0xB870L,0UL},{18446744073709551615UL,0x582EL,8UL,0x124DL,1UL},{6UL,-1L,7UL,3UL,0xB7L}}},{{{0x2EB9A52EL,0x9EFDL,0x49E1L,0xB224L,0x6FL},{0x2EB9A52EL,0x9EFDL,0x49E1L,0xB224L,0x6FL},{0xCB5D2F36L,5L,0xAAE3L,4UL,0x1FL},{1UL,-9L,0x4EAEL,0x67B1L,0x62L},{0x66A29370L,1L,65533UL,0x58A9L,0xE0L},{1UL,-9L,0x4EAEL,0x67B1L,0x62L},{0xCB5D2F36L,5L,0xAAE3L,4UL,0x1FL}},{{18446744073709551615UL,1L,0x9E3EL,0xDB7DL,1UL},{18446744073709551615UL,1L,0x9E3EL,0xDB7DL,1UL},{6UL,-1L,7UL,3UL,0xB7L},{18446744073709551615UL,0x582EL,8UL,0x124DL,1UL},{0xD58F413CL,0xF25BL,65531UL,0xB870L,0UL},{18446744073709551615UL,0x582EL,8UL,0x124DL,1UL},{6UL,-1L,7UL,3UL,0xB7L}},{{0x2EB9A52EL,0x9EFDL,0x49E1L,0xB224L,0x6FL},{0x2EB9A52EL,0x9EFDL,0x49E1L,0xB224L,0x6FL},{0xCB5D2F36L,5L,0xAAE3L,4UL,0x1FL},{1UL,-9L,0x4EAEL,0x67B1L,0x62L},{0x66A29370L,1L,65533UL,0x58A9L,0xE0L},{1UL,-9L,0x4EAEL,0x67B1L,0x62L},{0xCB5D2F36L,5L,0xAAE3L,4UL,0x1FL}},{{18446744073709551615UL,1L,0x9E3EL,0xDB7DL,1UL},{18446744073709551615UL,1L,0x9E3EL,0xDB7DL,1UL},{6UL,-1L,7UL,3UL,0xB7L},{18446744073709551615UL,0x582EL,8UL,0x124DL,1UL},{0xD58F413CL,0xF25BL,65531UL,0xB870L,0UL},{18446744073709551615UL,0x582EL,8UL,0x124DL,1UL},{6UL,-1L,7UL,3UL,0xB7L}}},{{{0x2EB9A52EL,0x9EFDL,0x49E1L,0xB224L,0x6FL},{0x2EB9A52EL,0x9EFDL,0x49E1L,0xB224L,0x6FL},{0xCB5D2F36L,5L,0xAAE3L,4UL,0x1FL},{1UL,-9L,0x4EAEL,0x67B1L,0x62L},{0x66A29370L,1L,65533UL,0x58A9L,0xE0L},{0xCB5D2F36L,5L,0xAAE3L,4UL,0x1FL},{0x66A29370L,1L,65533UL,0x58A9L,0xE0L}},{{18446744073709551612UL,-1L,1UL,0x787BL,0UL},{18446744073709551612UL,-1L,1UL,0x787BL,0UL},{0xD58F413CL,0xF25BL,65531UL,0xB870L,0UL},{6UL,-1L,7UL,3UL,0xB7L},{0UL,0x1B92L,0UL,0x6816L,0x23L},{6UL,-1L,7UL,3UL,0xB7L},{0xD58F413CL,0xF25BL,65531UL,0xB870L,0UL}},{{0xDD780AAAL,0L,65532UL,0x908CL,0xFDL},{0xDD780AAAL,0L,65532UL,0x908CL,0xFDL},{0x66A29370L,1L,65533UL,0x58A9L,0xE0L},{0xCB5D2F36L,5L,0xAAE3L,4UL,0x1FL},{5UL,0x69A3L,0x0436L,0x936DL,0xAAL},{0xCB5D2F36L,5L,0xAAE3L,4UL,0x1FL},{0x66A29370L,1L,65533UL,0x58A9L,0xE0L}},{{18446744073709551612UL,-1L,1UL,0x787BL,0UL},{18446744073709551612UL,-1L,1UL,0x787BL,0UL},{0xD58F413CL,0xF25BL,65531UL,0xB870L,0UL},{6UL,-1L,7UL,3UL,0xB7L},{0UL,0x1B92L,0UL,0x6816L,0x23L},{6UL,-1L,7UL,3UL,0xB7L},{0xD58F413CL,0xF25BL,65531UL,0xB870L,0UL}}},{{{0xDD780AAAL,0L,65532UL,0x908CL,0xFDL},{0xDD780AAAL,0L,65532UL,0x908CL,0xFDL},{0x66A29370L,1L,65533UL,0x58A9L,0xE0L},{0xCB5D2F36L,5L,0xAAE3L,4UL,0x1FL},{5UL,0x69A3L,0x0436L,0x936DL,0xAAL},{0xCB5D2F36L,5L,0xAAE3L,4UL,0x1FL},{0x66A29370L,1L,65533UL,0x58A9L,0xE0L}},{{18446744073709551612UL,-1L,1UL,0x787BL,0UL},{18446744073709551612UL,-1L,1UL,0x787BL,0UL},{0xD58F413CL,0xF25BL,65531UL,0xB870L,0UL},{6UL,-1L,7UL,3UL,0xB7L},{0UL,0x1B92L,0UL,0x6816L,0x23L},{6UL,-1L,7UL,3UL,0xB7L},{0xD58F413CL,0xF25BL,65531UL,0xB870L,0UL}},{{0xDD780AAAL,0L,65532UL,0x908CL,0xFDL},{0xDD780AAAL,0L,65532UL,0x908CL,0xFDL},{0x66A29370L,1L,65533UL,0x58A9L,0xE0L},{0xCB5D2F36L,5L,0xAAE3L,4UL,0x1FL},{5UL,0x69A3L,0x0436L,0x936DL,0xAAL},{0xCB5D2F36L,5L,0xAAE3L,4UL,0x1FL},{0x66A29370L,1L,65533UL,0x58A9L,0xE0L}},{{18446744073709551612UL,-1L,1UL,0x787BL,0UL},{18446744073709551612UL,-1L,1UL,0x787BL,0UL},{0xD58F413CL,0xF25BL,65531UL,0xB870L,0UL},{6UL,-1L,7UL,3UL,0xB7L},{0UL,0x1B92L,0UL,0x6816L,0x23L},{6UL,-1L,7UL,3UL,0xB7L},{0xD58F413CL,0xF25BL,65531UL,0xB870L,0UL}}},{{{0xDD780AAAL,0L,65532UL,0x908CL,0xFDL},{0xDD780AAAL,0L,65532UL,0x908CL,0xFDL},{0x66A29370L,1L,65533UL,0x58A9L,0xE0L},{0xCB5D2F36L,5L,0xAAE3L,4UL,0x1FL},{5UL,0x69A3L,0x0436L,0x936DL,0xAAL},{0xCB5D2F36L,5L,0xAAE3L,4UL,0x1FL},{0x66A29370L,1L,65533UL,0x58A9L,0xE0L}},{{18446744073709551612UL,-1L,1UL,0x787BL,0UL},{18446744073709551612UL,-1L,1UL,0x787BL,0UL},{0xD58F413CL,0xF25BL,65531UL,0xB870L,0UL},{6UL,-1L,7UL,3UL,0xB7L},{0UL,0x1B92L,0UL,0x6816L,0x23L},{6UL,-1L,7UL,3UL,0xB7L},{0xD58F413CL,0xF25BL,65531UL,0xB870L,0UL}},{{0xDD780AAAL,0L,65532UL,0x908CL,0xFDL},{0xDD780AAAL,0L,65532UL,0x908CL,0xFDL},{0x66A29370L,1L,65533UL,0x58A9L,0xE0L},{0xCB5D2F36L,5L,0xAAE3L,4UL,0x1FL},{5UL,0x69A3L,0x0436L,0x936DL,0xAAL},{0xCB5D2F36L,5L,0xAAE3L,4UL,0x1FL},{0x66A29370L,1L,65533UL,0x58A9L,0xE0L}},{{18446744073709551612UL,-1L,1UL,0x787BL,0UL},{18446744073709551612UL,-1L,1UL,0x787BL,0UL},{0xD58F413CL,0xF25BL,65531UL,0xB870L,0UL},{6UL,-1L,7UL,3UL,0xB7L},{0UL,0x1B92L,0UL,0x6816L,0x23L},{6UL,-1L,7UL,3UL,0xB7L},{0xD58F413CL,0xF25BL,65531UL,0xB870L,0UL}}}}; +static int8_t *g_512 = (void*)0; +static int8_t **g_511 = &g_512; +static int8_t g_537[3] = {(-4L),(-4L),(-4L)}; +static int32_t g_540 = 0xE36F71D3L; +static int32_t g_541 = (-4L); +static uint32_t g_542 = 1UL; +static int32_t g_553 = 0x2FE5E350L; +static uint8_t g_555[1][2] = {{0x6EL,0x6EL}}; +static const int32_t g_564 = 0x25327763L; +static uint16_t *g_601 = (void*)0; +static const int32_t *g_628 = &g_499[2][0]; +static const int32_t **g_627[6][9][4] = {{{&g_628,&g_628,(void*)0,&g_628},{&g_628,&g_628,(void*)0,&g_628},{(void*)0,&g_628,(void*)0,&g_628},{&g_628,&g_628,(void*)0,&g_628},{&g_628,&g_628,(void*)0,&g_628},{(void*)0,&g_628,(void*)0,&g_628},{&g_628,&g_628,(void*)0,&g_628},{&g_628,&g_628,(void*)0,&g_628},{(void*)0,&g_628,(void*)0,&g_628}},{{&g_628,&g_628,(void*)0,&g_628},{&g_628,&g_628,(void*)0,&g_628},{(void*)0,&g_628,(void*)0,&g_628},{&g_628,&g_628,(void*)0,&g_628},{&g_628,&g_628,(void*)0,&g_628},{(void*)0,&g_628,(void*)0,&g_628},{&g_628,&g_628,(void*)0,&g_628},{&g_628,&g_628,(void*)0,&g_628},{(void*)0,&g_628,(void*)0,&g_628}},{{&g_628,&g_628,(void*)0,&g_628},{&g_628,&g_628,(void*)0,&g_628},{(void*)0,&g_628,(void*)0,&g_628},{&g_628,&g_628,(void*)0,&g_628},{&g_628,&g_628,(void*)0,&g_628},{(void*)0,&g_628,(void*)0,&g_628},{&g_628,&g_628,(void*)0,&g_628},{&g_628,&g_628,(void*)0,&g_628},{(void*)0,&g_628,(void*)0,&g_628}},{{(void*)0,&g_628,&g_628,&g_628},{(void*)0,&g_628,(void*)0,&g_628},{(void*)0,&g_628,(void*)0,&g_628},{(void*)0,&g_628,&g_628,&g_628},{(void*)0,&g_628,(void*)0,&g_628},{(void*)0,&g_628,(void*)0,&g_628},{(void*)0,&g_628,&g_628,&g_628},{(void*)0,&g_628,(void*)0,&g_628},{(void*)0,&g_628,(void*)0,&g_628}},{{(void*)0,&g_628,&g_628,&g_628},{(void*)0,&g_628,(void*)0,&g_628},{(void*)0,&g_628,(void*)0,&g_628},{(void*)0,&g_628,&g_628,&g_628},{(void*)0,&g_628,(void*)0,&g_628},{(void*)0,&g_628,(void*)0,&g_628},{(void*)0,&g_628,&g_628,&g_628},{(void*)0,&g_628,(void*)0,&g_628},{(void*)0,&g_628,(void*)0,&g_628}},{{(void*)0,&g_628,&g_628,&g_628},{(void*)0,&g_628,(void*)0,&g_628},{(void*)0,&g_628,(void*)0,&g_628},{(void*)0,&g_628,&g_628,&g_628},{(void*)0,&g_628,(void*)0,&g_628},{(void*)0,&g_628,(void*)0,&g_628},{(void*)0,&g_628,&g_628,&g_628},{(void*)0,&g_628,(void*)0,&g_628},{(void*)0,&g_628,(void*)0,&g_628}}}; +static int32_t *g_672[8] = {&g_434,&g_2,&g_434,&g_2,&g_434,&g_2,&g_434,&g_2}; +static uint16_t g_802 = 1UL; +static int8_t g_809[5][1][8] = {{{(-1L),1L,(-1L),7L,0L,0x60L,0xEDL,0L}},{{0xA3L,(-1L),0xCFL,0x87L,0x50L,0L,0L,0L}},{{0xA3L,0L,0xA4L,0xA4L,0L,0xA3L,1L,7L}},{{(-1L),(-1L),0L,1L,0xCFL,(-6L),0L,0xEEL}},{{0x50L,7L,(-6L),1L,(-1L),1L,(-6L),7L}}}; +static uint32_t *g_825[3][2][1] = {{{&g_391.f9},{&g_391.f9}},{{&g_391.f9},{&g_391.f9}},{{&g_391.f9},{&g_391.f9}}}; +static int32_t g_876 = 0x2FE494ACL; +static const int8_t g_910 = 0x63L; +static uint16_t *** const g_962 = (void*)0; +static int16_t g_1052 = 0xF859L; +static uint16_t g_1105[9] = {0UL,0UL,0UL,0UL,0UL,0UL,0UL,0UL,0UL}; +static int32_t g_1109 = (-8L); +static struct S1 g_1113 = {2,990,1094,87,364,-7,-4072,0,16,0x351C817AL}; +static uint32_t *g_1142[1][6][3] = {{{&g_241,&g_18.f0,&g_408},{&g_209,&g_209,&g_408},{&g_18.f0,&g_241,&g_18.f0},{&g_195,&g_209,&g_195},{&g_195,&g_18.f0,&g_209},{&g_18.f0,&g_195,&g_195}}}; +static uint32_t **g_1141 = &g_1142[0][1][1]; +static struct S0 g_1169 = {8UL,0x2BA0L,1UL,65530UL,0x66L}; +static struct S0 *g_1168 = &g_1169; +static uint16_t g_1192[1][8] = {{0x03F8L,0x03F8L,0x03F8L,0x03F8L,0x03F8L,0x03F8L,0x03F8L,0x03F8L}}; +static int8_t g_1233[8] = {4L,0x9CL,4L,4L,0x9CL,4L,4L,0x9CL}; +static uint32_t g_1263 = 4294967287UL; +static int16_t **g_1272[8] = {&g_313[1][2][2],&g_313[1][2][2],&g_313[1][2][2],&g_313[1][2][2],&g_313[1][2][2],&g_313[1][2][2],&g_313[1][2][2],&g_313[1][2][2]}; +static int16_t ***g_1271[10] = {&g_1272[1],&g_1272[1],&g_1272[1],&g_1272[1],&g_1272[1],&g_1272[1],&g_1272[1],&g_1272[1],&g_1272[1],&g_1272[1]}; +static int16_t ****g_1270 = &g_1271[3]; +static int16_t g_1389 = 0xD9C6L; +static int8_t *g_1544[6] = {&g_40,&g_40,&g_40,&g_40,&g_40,&g_40}; + + +/* --- FORWARD DECLARATIONS --- */ +static uint16_t func_1(void); +static int32_t * func_6(int8_t p_7, int32_t * p_8); +static struct S0 func_9(int32_t * p_10, int32_t * p_11, int32_t * p_12); +static int32_t * func_13(const int16_t p_14, int32_t * p_15, int32_t * p_16); +static int32_t * func_19(int32_t * p_20, int32_t * p_21, struct S1 p_22); +static struct S1 func_24(uint8_t p_25, int8_t p_26, int32_t * p_27, int32_t p_28); +static uint32_t func_41(int32_t p_42, uint16_t * p_43, int32_t p_44, uint16_t p_45, int8_t * p_46); +static const uint16_t func_52(const int8_t * p_53, struct S0 p_54, uint16_t * p_55); +static const int8_t * func_56(int32_t * p_57, int16_t p_58, uint16_t * p_59, int8_t * p_60); +static uint32_t func_87(uint32_t p_88); + + +/* --- FUNCTIONS --- */ +/* ------------------------------------------ */ +/* + * reads : g_2 g_18 g_5 g_39 g_40 g_71 g_36 g_82 g_119 g_79 g_195 g_209 g_222 g_220 g_199 g_208 g_241 g_240 g_254 g_282 g_237 g_280 g_347 g_238 g_365 g_390 g_408 g_391.f4 g_391.f9 g_391 g_501 g_508.f0 g_541 g_508.f4 g_511 g_512 g_542 g_434 g_601 g_537 g_499 g_508.f2 g_627 g_251 g_508.f1 g_553 g_628 g_555 g_433 g_802 g_809 g_876 g_500 g_366 g_279 g_1105 g_1109 g_1192 g_1142 g_1389 g_1169.f1 g_508 g_1168 g_1169 g_1544 + * writes: g_2 g_5 g_18.f3 g_36 g_39 g_40 g_71 g_18.f4 g_82.f1 g_119 g_82.f4 g_79 g_195 g_199 g_209 g_82.f3 g_220 g_222 g_241 g_251 g_282 g_280 g_313 g_237 g_347 g_238 g_365 g_390 g_366 g_408 g_208 g_501 g_500 g_542 g_18.f1 g_434 g_254 g_541 g_672 g_433 g_279 g_825 g_809 g_876 g_540 g_537 g_802 g_555 g_1105 g_1109 g_18.f0 g_1113.f9 g_1141 g_82.f0 g_1168 g_1169.f3 g_1192 g_1389 g_601 g_1169.f1 g_240 + */ +static uint16_t func_1(void) +{ /* block id: 0 */ + const int32_t l_17 = 0x3AC9D73DL; + int32_t *l_23 = &g_2; + struct S1 l_33 = {2,-1611,502,54,563,-32,-5497,142,81,0xEA418246L}; + uint16_t *l_34 = &g_18.f3; + int8_t *l_35[4][3][10] = {{{&g_36[3],&g_36[3],&g_36[3],&g_36[3],&g_36[3],(void*)0,&g_36[3],&g_36[7],&g_36[0],&g_36[7]},{&g_36[3],(void*)0,&g_36[3],&g_36[7],&g_36[3],(void*)0,&g_36[3],&g_36[3],&g_36[0],&g_36[6]},{&g_36[3],(void*)0,&g_36[3],&g_36[3],&g_36[3],&g_36[3],&g_36[3],&g_36[3],(void*)0,&g_36[3]}},{{&g_36[3],(void*)0,&g_36[0],&g_36[3],&g_36[7],&g_36[6],&g_36[3],&g_36[6],&g_36[7],&g_36[3]},{&g_36[3],(void*)0,&g_36[3],(void*)0,&g_36[7],&g_36[3],&g_36[3],&g_36[3],&g_36[3],&g_36[3]},{&g_36[7],&g_36[3],&g_36[3],&g_36[3],&g_36[3],&g_36[7],&g_36[3],&g_36[3],&g_36[0],&g_36[3]}},{{&g_36[3],&g_36[3],&g_36[3],&g_36[3],&g_36[3],&g_36[3],&g_36[3],&g_36[3],&g_36[3],&g_36[3]},{&g_36[3],(void*)0,&g_36[7],&g_36[3],&g_36[3],&g_36[3],&g_36[3],&g_36[3],&g_36[3],&g_36[7]},{&g_36[3],&g_36[3],&g_36[3],&g_36[3],&g_36[7],(void*)0,&g_36[3],(void*)0,&g_36[3],(void*)0}},{{&g_36[3],&g_36[3],&g_36[3],&g_36[3],&g_36[3],&g_36[3],&g_36[3],&g_36[3],&g_36[0],&g_36[0]},{&g_36[3],&g_36[3],&g_36[6],(void*)0,(void*)0,&g_36[6],&g_36[3],&g_36[3],&g_36[3],&g_36[0]},{(void*)0,(void*)0,&g_36[3],&g_36[3],&g_36[3],&g_36[3],&g_36[3],&g_36[3],&g_36[3],(void*)0}}}; + int32_t *l_1108 = &g_1109; + int32_t *l_1545 = &g_238; + uint16_t l_1556 = 0x4844L; + int32_t *l_1557 = &g_433; + struct S0 *l_1560 = &g_1169; + int i, j, k; + for (g_2 = 0; (g_2 == 11); g_2++) + { /* block id: 3 */ + g_5 = g_2; + } + l_1545 = func_6(((func_9(func_13(l_17, (g_18 , func_19(l_23, l_23, func_24((*l_23), (0x2DC8L <= (g_18.f4 <= (safe_mod_func_int16_t_s_s(((((g_36[3] = ((safe_mul_func_int8_t_s_s((l_33 , (((*l_34) = (&g_2 == &l_17)) ^ g_2)), (*l_23))) >= (*l_23))) != 0x58L) != 0UL) , g_5), 3L)))), &g_2, g_5))), l_1108), g_1142[0][2][2], g_1142[0][1][1]) , (*l_1108)) & g_508[3][3][4].f2), l_1108); + (*l_1108) = ((*g_390) , ((safe_mod_func_int32_t_s_s(0xE4015B19L, ((*l_1557) |= ((safe_mul_func_int16_t_s_s((*l_1545), (*l_1545))) == ((safe_add_func_int16_t_s_s((safe_mul_func_int8_t_s_s((safe_div_func_uint16_t_u_u((*l_1108), (*l_23))), 0xE0L)), 0x3E3DL)) && 0x0202L))))) < (*l_23))); + for (g_240 = 0; (g_240 < (-25)); g_240 = safe_sub_func_int32_t_s_s(g_240, 6)) + { /* block id: 1024 */ + struct S0 **l_1561 = &g_1168; + (*l_1561) = l_1560; + } + return (*l_23); +} + + +/* ------------------------------------------ */ +/* + * reads : g_82.f4 g_628 g_499 g_2 g_1109 g_220 g_1544 + * writes: g_82.f4 g_82.f1 g_500 g_1109 g_2 g_199 + */ +static int32_t * func_6(int8_t p_7, int32_t * p_8) +{ /* block id: 996 */ + int32_t l_1514 = (-7L); + int32_t l_1517[10][3][7] = {{{0x6027BB7EL,0xECF50607L,0xECF50607L,0x6027BB7EL,0x6027BB7EL,0xECF50607L,0xECF50607L},{0x2C43085FL,(-1L),0x2C43085FL,(-1L),0x2C43085FL,(-1L),0x2C43085FL},{0x6027BB7EL,0x6027BB7EL,0xECF50607L,0xECF50607L,0x6027BB7EL,0x6027BB7EL,0xECF50607L}},{{0xD643F3D2L,(-1L),0xD643F3D2L,(-1L),0xD643F3D2L,(-1L),0xD643F3D2L},{0x6027BB7EL,0xECF50607L,0xECF50607L,0x6027BB7EL,0x6027BB7EL,0xECF50607L,0xECF50607L},{0x2C43085FL,(-1L),0x2C43085FL,(-1L),0x2C43085FL,(-1L),0x2C43085FL}},{{0x6027BB7EL,0x6027BB7EL,0xECF50607L,0xECF50607L,0x6027BB7EL,0x6027BB7EL,0xECF50607L},{0xD643F3D2L,(-1L),0xD643F3D2L,(-1L),0xD643F3D2L,(-1L),0xD643F3D2L},{0x6027BB7EL,0xECF50607L,0xECF50607L,0x6027BB7EL,0x6027BB7EL,0xECF50607L,0xECF50607L}},{{0x2C43085FL,(-1L),0x2C43085FL,(-1L),0x2C43085FL,(-1L),0x2C43085FL},{0x6027BB7EL,0x6027BB7EL,0xECF50607L,0xECF50607L,0x6027BB7EL,0x6027BB7EL,0xECF50607L},{0xD643F3D2L,(-1L),0xD643F3D2L,(-1L),0xD643F3D2L,(-1L),0xD643F3D2L}},{{0x6027BB7EL,0xECF50607L,0xECF50607L,0x6027BB7EL,0x6027BB7EL,0xECF50607L,0xECF50607L},{0x2C43085FL,(-1L),0x2C43085FL,(-1L),0x2C43085FL,(-1L),0x2C43085FL},{0x6027BB7EL,0x6027BB7EL,0xECF50607L,0xECF50607L,0x6027BB7EL,0x6027BB7EL,0xECF50607L}},{{0xD643F3D2L,(-1L),0xD643F3D2L,(-1L),0xD643F3D2L,(-1L),0xD643F3D2L},{0x6027BB7EL,0xECF50607L,0xECF50607L,0x6027BB7EL,0x6027BB7EL,0xECF50607L,0xECF50607L},{0x2C43085FL,(-1L),0x2C43085FL,(-1L),0x2C43085FL,(-1L),0x2C43085FL}},{{0x6027BB7EL,0x6027BB7EL,0xECF50607L,0xECF50607L,0x6027BB7EL,0x6027BB7EL,0xECF50607L},{0xD643F3D2L,(-1L),0xD643F3D2L,(-1L),0xD643F3D2L,(-1L),0xD643F3D2L},{0x6027BB7EL,0xECF50607L,0xECF50607L,0x6027BB7EL,0x6027BB7EL,0xECF50607L,0xECF50607L}},{{0x2C43085FL,(-1L),0x2C43085FL,(-1L),0x2C43085FL,(-1L),0x2C43085FL},{0x6027BB7EL,0x6027BB7EL,0xECF50607L,0xECF50607L,0x6027BB7EL,0x6027BB7EL,0xECF50607L},{0xD643F3D2L,(-1L),0xD643F3D2L,(-1L),0xD643F3D2L,(-1L),0xD643F3D2L}},{{0x6027BB7EL,0xECF50607L,0xECF50607L,0x6027BB7EL,0x6027BB7EL,0xECF50607L,0xECF50607L},{0x2C43085FL,(-1L),0x2C43085FL,(-1L),0x2C43085FL,(-1L),0x2C43085FL},{0x6027BB7EL,0x6027BB7EL,0xECF50607L,0xECF50607L,0x6027BB7EL,0x6027BB7EL,0xECF50607L}},{{0xD643F3D2L,(-1L),0xD643F3D2L,(-1L),0xD643F3D2L,(-1L),0xD643F3D2L},{0x6027BB7EL,0xECF50607L,0xECF50607L,0x6027BB7EL,0x6027BB7EL,0xECF50607L,0xECF50607L},{0x2C43085FL,(-1L),0x2C43085FL,(-1L),0x2C43085FL,(-1L),0x2C43085FL}}}; + uint16_t *l_1521 = (void*)0; + int8_t *l_1522 = &g_537[0]; + const struct S1 l_1523 = {2,-1323,441,77,102,35,-3984,6,78,0UL}; + int32_t l_1526 = 0x2465F014L; + int16_t l_1533 = 0x3A98L; + int16_t l_1535 = (-5L); + int i, j, k; + for (g_82.f4 = 21; (g_82.f4 > 18); g_82.f4--) + { /* block id: 999 */ + int8_t l_1518 = 1L; + int16_t l_1524 = (-3L); + uint32_t *l_1525 = (void*)0; + (*p_8) = (safe_add_func_int32_t_s_s((func_41((((l_1526 ^= ((((*p_8) = ((((l_1514 == (p_7 ^ (0xEEC6D689L == (safe_lshift_func_int8_t_s_s(((l_1517[8][1][6] |= p_7) != (l_1518 ^ ((~(l_1518 != ((((-3L) < (safe_mul_func_uint16_t_u_u(func_41(p_7, l_1521, p_7, l_1518, l_1522), (-1L)))) , l_1523) , p_7))) || 0xB339L))), l_1518))))) || 0UL) && l_1518) & 4294967295UL)) , l_1524) == 65532UL)) , p_7) , p_7), l_1521, (*g_628), p_7, &l_1518) || 4294967292UL), 1UL)); + } + for (g_2 = 0; (g_2 > 27); g_2++) + { /* block id: 1007 */ + int32_t *l_1529 = &l_1514; + int32_t *l_1530 = &g_434; + int32_t *l_1531 = &g_237; + int32_t *l_1532[7][5] = {{&g_434,&g_2,&g_434,(void*)0,(void*)0},{&g_434,&g_2,&g_434,(void*)0,(void*)0},{&g_434,&g_2,(void*)0,&l_1514,&l_1514},{(void*)0,&l_1517[4][0][6],(void*)0,&l_1514,&l_1514},{(void*)0,&l_1517[4][0][6],(void*)0,&l_1514,&l_1514},{(void*)0,&l_1517[4][0][6],(void*)0,&l_1514,&l_1514},{(void*)0,&l_1517[4][0][6],(void*)0,&l_1514,&l_1514}}; + int8_t l_1534 = (-1L); + uint8_t l_1536[1][7][7] = {{{0xD3L,0UL,7UL,0UL,0UL,7UL,0UL},{0UL,255UL,0x9EL,0UL,255UL,0x1AL,0UL},{248UL,0UL,0x1AL,248UL,0UL,248UL,0x1AL},{8UL,8UL,0x0EL,0UL,0UL,255UL,8UL},{8UL,0x1AL,0x9EL,0UL,0x74L,0x74L,0UL},{248UL,0UL,248UL,0x1AL,0UL,248UL,255UL},{0UL,0UL,0xE9L,0UL,0UL,0x0EL,0UL}}}; + int i, j, k; + if ((*p_8)) + break; + ++l_1536[0][2][1]; + } + if ((*p_8)) + { /* block id: 1011 */ + (*g_220) = p_8; + } + else + { /* block id: 1013 */ + struct S1 *l_1539 = &g_391; + struct S1 **l_1540 = (void*)0; + struct S1 **l_1541 = &l_1539; + uint16_t *l_1542 = &g_82.f3; + int32_t l_1543 = 0x3FE0E73CL; + (*l_1541) = l_1539; + (*p_8) &= func_41(p_7, l_1542, l_1526, l_1543, g_1544[5]); + } + l_1514 = (*p_8); + return p_8; +} + + +/* ------------------------------------------ */ +/* + * reads : g_209 g_1192 g_408 g_18.f0 g_195 g_241 g_511 g_512 g_1105 g_82.f2 g_555 g_1389 g_40 g_628 g_499 g_18.f4 g_82.f1 g_1169.f1 g_508 g_220 g_1168 g_1169 + * writes: g_209 g_1169.f3 g_1192 g_408 g_18.f0 g_195 g_241 g_82.f1 g_500 g_555 g_1389 g_40 g_601 g_18.f4 g_1169.f1 g_199 g_390 + */ +static struct S0 func_9(int32_t * p_10, int32_t * p_11, int32_t * p_12) +{ /* block id: 895 */ + uint32_t l_1289 = 3UL; + uint8_t ** const *l_1291 = &g_254; + uint8_t ** const ** const l_1290 = &l_1291; + int32_t l_1335 = 1L; + int32_t l_1342 = 0x122D1585L; + int32_t l_1344[2]; + uint16_t l_1391 = 0x4F32L; + uint32_t *l_1400[6] = {&g_391.f9,&g_391.f9,&g_391.f9,&g_391.f9,&g_391.f9,&g_391.f9}; + int8_t l_1402 = (-1L); + const struct S0 *l_1417 = &g_508[3][3][4]; + uint32_t *l_1424 = &l_1289; + uint32_t **l_1427 = &g_1142[0][5][2]; + uint32_t l_1429 = 0x32A2A297L; + int8_t *l_1447 = &g_279; + int32_t l_1460 = 1L; + const int16_t l_1468 = 1L; + int32_t l_1498 = 8L; + int i; + for (i = 0; i < 2; i++) + l_1344[i] = 0xC9F0AD98L; + if ((l_1289 && (l_1290 != &l_1291))) + { /* block id: 896 */ + uint8_t l_1292[1]; + int32_t l_1327 = 0x2CA2ED02L; + int32_t l_1336[5][6] = {{0L,(-6L),0x2325E39FL,0xAF70E644L,0x06C4E62EL,(-1L)},{0xDD12AB3AL,0x1CD1E72DL,(-1L),0x06C4E62EL,(-1L),0x1CD1E72DL},{0xDD12AB3AL,(-1L),0x06C4E62EL,0xAF70E644L,0x2325E39FL,(-6L)},{0xDFD6990FL,(-1L),(-1L),(-6L),(-6L),(-1L)},{(-1L),(-1L),0x738CE679L,0xDFD6990FL,0x06C4E62EL,(-6L)}}; + uint16_t *l_1360 = (void*)0; + uint32_t l_1368 = 0x88AF1839L; + int8_t *l_1382[10][3][7] = {{{&g_809[0][0][0],(void*)0,(void*)0,&g_809[4][0][6],&g_279,&g_809[0][0][0],&g_809[4][0][6]},{&g_36[0],&g_36[7],&g_537[0],&g_537[1],&g_537[2],(void*)0,(void*)0},{&g_537[2],&g_809[0][0][0],&g_1233[1],&g_809[0][0][0],&g_537[2],&g_809[0][0][0],&g_36[4]}},{{&g_1233[0],(void*)0,&g_537[1],&g_36[4],&g_279,(void*)0,&g_1233[0]},{&g_537[0],&g_1233[0],(void*)0,&g_279,&g_36[4],&g_537[1],(void*)0},{&g_1233[0],&g_36[4],&g_809[0][0][0],&g_537[2],&g_809[0][0][0],&g_1233[1],&g_809[0][0][0]}},{{&g_537[2],(void*)0,(void*)0,&g_537[2],&g_537[1],&g_537[0],&g_36[7]},{&g_36[0],&g_809[4][0][6],&g_809[0][0][0],&g_279,&g_809[4][0][6],(void*)0,(void*)0},{&g_809[0][0][0],&g_809[0][0][0],&g_1233[1],&g_36[4],&g_1233[1],&g_1233[1],&g_36[7]}},{{&g_809[0][0][0],(void*)0,&g_36[0],&g_809[0][0][0],&g_279,&g_279,&g_809[0][0][0]},{&g_36[0],&g_537[2],&g_36[0],&g_537[1],&g_809[0][0][0],&g_279,(void*)0},{&g_809[0][0][0],&g_809[0][0][0],&g_1233[1],&g_809[4][0][6],&g_36[7],&g_809[0][0][0],&g_279}},{{(void*)0,&g_809[4][0][5],(void*)0,(void*)0,(void*)0,&g_280,(void*)0},{&g_809[0][0][0],(void*)0,&g_809[4][0][5],(void*)0,&g_279,(void*)0,&g_809[4][0][5]},{(void*)0,(void*)0,&g_537[2],&g_537[1],&g_537[0],&g_36[7],&g_36[0]}},{{&g_809[0][0][0],&g_809[4][0][5],&g_36[3],&g_809[0][0][0],&g_280,&g_36[3],&g_537[1]},{&g_1233[1],&g_537[0],&g_280,(void*)0,&g_537[0],&g_809[0][0][0],&g_36[3]},{&g_36[0],&g_279,&g_36[7],&g_279,&g_279,&g_36[7],&g_279}},{{&g_36[0],&g_36[3],&g_809[0][0][0],&g_537[0],(void*)0,&g_280,&g_537[0]},{&g_1233[1],&g_537[1],&g_36[3],&g_280,&g_809[0][0][0],&g_36[3],&g_809[4][0][5]},{&g_809[0][0][0],&g_36[0],&g_36[7],&g_537[0],&g_537[1],&g_537[2],(void*)0}},{{(void*)0,&g_809[4][0][5],(void*)0,&g_279,(void*)0,&g_809[4][0][5],(void*)0},{&g_809[0][0][0],(void*)0,&g_280,(void*)0,(void*)0,(void*)0,&g_809[4][0][5]},{(void*)0,&g_279,&g_537[2],&g_809[0][0][0],&g_36[0],&g_36[7],&g_537[0]}},{{&g_537[1],&g_809[4][0][5],&g_280,&g_537[1],&g_280,&g_1233[1],&g_279},{&g_1233[1],(void*)0,(void*)0,&g_280,&g_809[0][0][0],(void*)0,&g_1233[1]},{&g_537[1],(void*)0,&g_36[4],(void*)0,&g_36[0],&g_36[4],(void*)0}},{{&g_809[0][0][0],&g_1233[1],(void*)0,&g_279,&g_280,&g_36[3],&g_279},{(void*)0,(void*)0,&g_280,&g_809[4][0][5],&g_279,&g_36[3],&g_36[3]},{&g_279,&g_809[0][0][0],&g_36[4],&g_809[0][0][0],&g_279,&g_1233[0],&g_36[0]}}}; + int32_t l_1401 = (-9L); + int i, j, k; + for (i = 0; i < 1; i++) + l_1292[i] = 1UL; + ++l_1292[0]; + for (g_209 = 22; (g_209 > 44); g_209 = safe_add_func_uint16_t_u_u(g_209, 7)) + { /* block id: 900 */ + uint8_t l_1302 = 250UL; + int32_t l_1322 = 0x6B975B0CL; + int32_t l_1343[7][3] = {{0L,0L,(-2L)},{0xB97A3A68L,0x0944A5F0L,0x2461D461L},{0L,0L,0x2461D461L},{0x0944A5F0L,0xB97A3A68L,(-2L)},{0L,0L,0L},{0L,0x0944A5F0L,0L},{0x0944A5F0L,0L,0L}}; + int16_t l_1346[7][1][10] = {{{1L,8L,8L,1L,0x36ECL,0x3A6CL,6L,0xE8B3L,1L,1L}},{{0xE8B3L,0x691FL,6L,0x36ECL,6L,0xB5D7L,8L,6L,1L,0x3A6CL}},{{(-1L),1L,0x7EDEL,1L,0x72E8L,0x7AB0L,0x72E8L,1L,0x7EDEL,1L}},{{0L,0xB5D7L,0x3A6CL,0x3EA9L,(-10L),0x36ECL,0x3EA9L,0xE8B3L,0x29FAL,0L}},{{0x3A6CL,(-1L),0xB5D7L,0x8336L,0xB5D7L,(-7L),0xE7DBL,0x29FAL,0x1158L,0x1158L}},{{0xE8B3L,(-10L),6L,0x29FAL,0x29FAL,6L,(-10L),0xE8B3L,0x691FL,6L}},{{0x29FAL,0xE7DBL,(-7L),0xB5D7L,0x8336L,1L,0x29FAL,0x7EDEL,0x7AB0L,0x29FAL}}}; + uint16_t *l_1358 = (void*)0; + int8_t *l_1390 = &g_280; + int32_t l_1403 = 2L; + int i, j, k; + } + } + else + { /* block id: 946 */ + uint16_t *l_1425 = (void*)0; + uint16_t *l_1426 = &g_1169.f3; + int32_t l_1428 = 0x895941A3L; + uint16_t *l_1430 = &g_1192[0][7]; + int32_t l_1431 = (-7L); + int32_t *l_1436[8][2][2] = {{{&l_1335,&l_1335},{&l_1335,&l_1431}},{{&g_39,&g_238},{&l_1431,&g_238}},{{&g_39,&l_1431},{&l_1335,&l_1335}},{{&l_1335,&l_1431},{&g_39,&g_238}},{{&l_1431,&g_238},{&g_39,&l_1431}},{{&l_1335,&l_1335},{&l_1335,&l_1431}},{{&g_39,&g_238},{&l_1431,&g_238}},{{&g_39,&l_1431},{&l_1335,&l_1335}}}; + struct S0 l_1477 = {1UL,1L,0x5EBBL,0xA03BL,0x3FL}; + int i, j, k; + (*p_12) = ((((*l_1426) = (p_11 == (l_1424 = p_11))) | (l_1427 != &g_1142[0][0][2])) > ((l_1428 , l_1429) != ((((l_1431 = ((l_1428 >= (((l_1344[1] , (((*l_1430) ^= l_1344[1]) < 0x71D3L)) <= l_1289) < 1L)) , 249UL)) | l_1344[1]) | 0x92L) , l_1344[1]))); + (*p_11) = (safe_mod_func_int16_t_s_s(l_1431, (--(*l_1430)))); + l_1436[7][1][0] = (((l_1428 ^ 0xAFL) & ((*p_12) && l_1431)) , &l_1431); + if (((*p_12) = (safe_lshift_func_int8_t_s_u((((safe_sub_func_int8_t_s_s(func_41((safe_lshift_func_uint16_t_u_s((((((&l_1417 == ((0xF7A9L > (l_1335 == (*p_11))) , (void*)0)) <= func_41((safe_mul_func_uint16_t_u_u(l_1289, l_1429)), &g_1192[0][2], (safe_mod_func_uint8_t_u_u((l_1402 && l_1335), l_1429)), l_1344[1], (*g_511))) , l_1289) > l_1402) , l_1344[1]), 14)), &g_222, l_1391, l_1335, l_1447), g_1105[8])) | l_1289) && 0L), 4)))) + { /* block id: 956 */ + uint32_t l_1452 = 0x19EBA21BL; + int32_t l_1457 = 5L; + uint8_t *l_1465 = &g_555[0][0]; + const uint16_t **l_1478[2]; + uint16_t l_1481 = 0xF23DL; + int i; + for (i = 0; i < 2; i++) + l_1478[i] = (void*)0; + (*p_12) = ((safe_add_func_int8_t_s_s((safe_add_func_int8_t_s_s(l_1452, l_1402)), (safe_mod_func_int16_t_s_s((safe_rshift_func_uint8_t_u_u((l_1457 &= g_82.f2), (((safe_lshift_func_uint8_t_u_u(l_1342, 2)) , func_41((((l_1460 || ((safe_mul_func_uint16_t_u_u(((safe_div_func_int8_t_s_s(((~(++(*l_1465))) == func_41(l_1452, l_1430, l_1452, l_1452, l_1465)), l_1452)) && l_1391), 0x642EL)) & 0x6C9408A5L)) & l_1468) == 0x97L), &l_1391, l_1335, l_1452, (*g_511))) == 3L))), l_1468)))) , (*p_12)); + for (g_1389 = 0; (g_1389 < (-25)); g_1389--) + { /* block id: 962 */ + uint16_t *l_1476 = (void*)0; + const int32_t l_1479[9] = {0x7870A31CL,0x7870A31CL,0x7870A31CL,0x7870A31CL,0x7870A31CL,0x7870A31CL,0x7870A31CL,0x7870A31CL,0x7870A31CL}; + int32_t l_1480 = 0xBD654D60L; + int i; + for (g_40 = 0; (g_40 <= 2); g_40 += 1) + { /* block id: 965 */ + uint16_t l_1475 = 1UL; + if ((*p_11)) + break; + l_1480 = (safe_mod_func_int32_t_s_s((safe_rshift_func_int8_t_s_s(((((func_41(l_1475, (l_1452 , (g_601 = l_1476)), (*g_628), (&g_1142[0][1][1] == &g_1142[0][1][1]), (l_1477 , (*g_511))) > 0xEAA0L) , &g_601) == l_1478[0]) , l_1479[0]), 3)), 1UL)); + --l_1481; + } + } + } + else + { /* block id: 972 */ + int32_t l_1490 = 0xBC1D070AL; + int32_t l_1495 = 0x55B4B91BL; + int32_t l_1496 = 0L; + int32_t l_1497[1][1][5] = {{{5L,5L,5L,5L,5L}}}; + uint16_t l_1506 = 0x8821L; + struct S1 **l_1509[2]; + int i, j, k; + for (i = 0; i < 2; i++) + l_1509[i] = &g_390; + for (g_18.f4 = 0; (g_18.f4 < 54); g_18.f4 = safe_add_func_uint8_t_u_u(g_18.f4, 5)) + { /* block id: 975 */ + for (g_82.f1 = 6; (g_82.f1 > 20); g_82.f1 = safe_add_func_uint32_t_u_u(g_82.f1, 1)) + { /* block id: 978 */ + for (g_1169.f1 = (-2); (g_1169.f1 != (-22)); g_1169.f1 = safe_sub_func_uint16_t_u_u(g_1169.f1, 3)) + { /* block id: 981 */ + return (*l_1417); + } + } + } + if ((l_1490 ^ (safe_div_func_uint16_t_u_u(l_1342, l_1490)))) + { /* block id: 986 */ + (*p_12) = (*p_12); + } + else + { /* block id: 988 */ + int32_t l_1493[3][3] = {{(-4L),(-4L),(-1L)},{(-4L),(-4L),(-1L)},{(-4L),(-4L),(-1L)}}; + int32_t l_1494 = 0xF56B557AL; + int32_t l_1499 = 0L; + int32_t l_1500 = 0x64A61DCEL; + int32_t l_1501 = 0x18BA8B7FL; + int32_t l_1502 = 0xFD7B4B2BL; + int32_t l_1503 = 0x1CDE3638L; + int32_t l_1504 = 1L; + int32_t l_1505 = (-1L); + int i, j; + ++l_1506; + } + (*g_220) = p_12; + g_390 = &g_391; + } + } + return (*g_1168); +} + + +/* ------------------------------------------ */ +/* + * reads : g_2 g_1109 g_82 g_802 g_220 g_390 g_391 g_18 g_542 g_555 g_119 g_1105 g_199 g_279 g_501 g_499 g_5 g_1192 g_251 + * writes: g_1109 g_18.f0 g_2 g_802 g_1113.f9 g_1141 g_279 g_199 g_82.f4 g_82.f0 g_542 g_555 g_119 g_1105 g_390 g_501 g_1168 g_5 g_251 g_82.f3 + */ +static int32_t * func_13(const int16_t p_14, int32_t * p_15, int32_t * p_16) +{ /* block id: 748 */ + uint32_t l_1116[9][10] = {{0x90628E09L,0xB05BB543L,0xB05BB543L,0x90628E09L,6UL,0x7AA97B2BL,0x2750F53DL,0UL,0x5A6596A4L,0UL},{0xB05BB543L,4294967294UL,0x0D9C2F16L,0x0D9C2F16L,6UL,0xCA63206FL,0x2ADE30DFL,4294967294UL,0UL,0x90628E09L},{4294967294UL,0xB05BB543L,0x2750F53DL,0x5A6596A4L,0x2ADE30DFL,0x2ADE30DFL,0x5A6596A4L,0x2750F53DL,0xB05BB543L,4294967294UL},{0x71DF7270L,0xB05BB543L,0UL,4294967293UL,0x0D9C2F16L,0x90628E09L,0x2ADE30DFL,0x90628E09L,0x0D9C2F16L,4294967293UL},{4294967293UL,0xCA63206FL,4294967293UL,0xB05BB543L,0x0D9C2F16L,0x7AA97B2BL,4294967294UL,6UL,6UL,4294967294UL},{0x0D9C2F16L,0x2ADE30DFL,0x7AA97B2BL,0x7AA97B2BL,0x2ADE30DFL,0x0D9C2F16L,0x71DF7270L,6UL,0x5A6596A4L,0x90628E09L},{0xCA63206FL,0x90628E09L,4294967293UL,0x2750F53DL,6UL,0x2750F53DL,4294967293UL,0x90628E09L,0xCA63206FL,0x0D9C2F16L},{0xCA63206FL,0x7AA97B2BL,0UL,0x71DF7270L,0x2750F53DL,0x0D9C2F16L,0x0D9C2F16L,0x2750F53DL,0x71DF7270L,0UL},{0x0D9C2F16L,0x0D9C2F16L,0x2750F53DL,0x71DF7270L,0UL,0x7AA97B2BL,0xCA63206FL,4294967294UL,0xCA63206FL,0x7AA97B2BL}}; + int8_t *l_1119 = &g_537[0]; + int32_t l_1122 = (-1L); + int32_t l_1126 = (-1L); + struct S1 **l_1130 = &g_390; + struct S1 ***l_1129 = &l_1130; + int32_t l_1178 = 0x6946CE07L; + int32_t l_1180[2]; + int8_t l_1196[3][10][5] = {{{(-5L),(-1L),0xEDL,(-1L),(-5L)},{0L,4L,0x17L,0xFCL,0x93L},{0x89L,0x46L,0x5DL,(-1L),(-1L)},{0xFCL,1L,0xFCL,(-1L),0xE8L},{5L,(-1L),0xEDL,0x5DL,0x89L},{0xE8L,0x89L,4L,4L,0x89L},{0x43L,9L,0xEDL,0x89L,0x46L},{0xFCL,0xF4L,0L,0xBFL,0xC4L},{0x0FL,0x43L,0x43L,0x0FL,0L},{0xFCL,4L,0x93L,0x42L,0xBFL}},{{0x43L,(-5L),(-1L),0xEDL,(-1L)},{0xE8L,0xE8L,0xBFL,0x42L,0x93L},{5L,0x5DL,0L,0x0FL,0x43L},{0L,0xBFL,0xC4L,0xBFL,0L},{(-1L),0x5DL,0x46L,0x89L,0xEDL},{0x42L,0xE8L,0x89L,4L,4L},{0x89L,(-5L),0x89L,0x5DL,0xEDL},{1L,4L,0xE8L,(-1L),0L},{0xEDL,0x43L,(-1L),(-1L),0x43L},{0x89L,0xF4L,0xE8L,0L,0x93L}},{{(-5L),9L,0x89L,0L,(-1L)},{0x17L,0x89L,0x89L,0x17L,0xBFL},{(-5L),(-1L),0x46L,(-1L),5L},{0xF4L,0L,0xBFL,0xC4L,0xBFL},{(-1L),(-1L),5L,(-1L),0xEDL},{0xFCL,0x89L,1L,0x93L,0xF4L},{(-1L),5L,0L,5L,(-1L)},{4L,0x89L,0xE8L,0x42L,0xC4L},{(-1L),(-1L),9L,0x5DL,0x5DL},{0x42L,0L,0x42L,0x89L,0xC4L}}}; + int8_t l_1197 = (-1L); + uint32_t *l_1205 = &g_251; + uint8_t *l_1210 = &g_119; + int16_t *l_1215[2][10][4] = {{{&g_18.f1,&g_18.f1,&g_18.f1,(void*)0},{&g_18.f1,(void*)0,(void*)0,&g_18.f1},{&g_1052,(void*)0,(void*)0,(void*)0},{&g_18.f1,&g_18.f1,&g_18.f1,&g_18.f1},{&g_18.f1,(void*)0,&g_1052,&g_18.f1},{&g_1052,&g_18.f1,(void*)0,(void*)0},{(void*)0,(void*)0,&g_18.f1,&g_18.f1},{(void*)0,(void*)0,(void*)0,(void*)0},{&g_1052,&g_18.f1,&g_1052,(void*)0},{&g_18.f1,&g_18.f1,&g_18.f1,(void*)0}},{{&g_18.f1,(void*)0,(void*)0,&g_18.f1},{&g_1052,(void*)0,(void*)0,(void*)0},{&g_18.f1,&g_18.f1,&g_18.f1,&g_18.f1},{&g_18.f1,(void*)0,&g_1052,&g_18.f1},{&g_1052,&g_18.f1,(void*)0,&g_18.f1},{(void*)0,(void*)0,&g_18.f1,(void*)0},{(void*)0,&g_508[3][3][4].f1,&g_18.f1,(void*)0},{&g_18.f1,(void*)0,&g_18.f1,&g_18.f1},{&g_1052,(void*)0,&g_18.f1,(void*)0},{(void*)0,&g_508[3][3][4].f1,&g_508[3][3][4].f1,(void*)0}}}; + int16_t l_1216 = (-4L); + int32_t l_1250 = (-5L); + int8_t l_1260 = (-10L); + int32_t l_1261 = 5L; + int32_t *l_1280[8][10] = {{&g_39,&g_39,&l_1126,&g_238,(void*)0,&g_39,&g_876,&l_1126,(void*)0,(void*)0},{&g_39,&g_876,&l_1126,(void*)0,(void*)0,&g_876,&g_876,(void*)0,(void*)0,&l_1126},{&g_39,&g_39,&l_1126,&g_238,(void*)0,&g_39,&g_876,&l_1126,(void*)0,(void*)0},{&g_39,&g_876,&l_1126,(void*)0,(void*)0,&g_876,&g_876,(void*)0,(void*)0,&l_1126},{&g_39,&g_39,&l_1126,&g_238,(void*)0,&g_39,&g_876,&l_1126,(void*)0,(void*)0},{&g_39,&g_876,&l_1126,(void*)0,(void*)0,&g_876,&g_876,(void*)0,(void*)0,&l_1126},{&g_39,&g_39,&l_1126,&g_238,(void*)0,&g_39,&g_876,&l_1126,(void*)0,(void*)0},{&g_39,&g_876,&l_1126,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,&g_39,&g_238}}; + struct S0 **l_1283 = &g_1168; + int32_t *l_1287 = (void*)0; + int32_t *l_1288 = &l_1178; + int i, j, k; + for (i = 0; i < 2; i++) + l_1180[i] = 0x8AE8E413L; + if ((*p_15)) + { /* block id: 749 */ + struct S1 *l_1110 = &g_391; + struct S1 **l_1111[8][5][5] = {{{&l_1110,(void*)0,&g_390,&g_390,&g_390},{&l_1110,&l_1110,&l_1110,&l_1110,&l_1110},{&l_1110,&g_390,&g_390,&l_1110,&g_390},{&l_1110,&g_390,&g_390,&g_390,&g_390},{&l_1110,&g_390,&l_1110,&l_1110,&g_390}},{{(void*)0,&l_1110,&g_390,&l_1110,&l_1110},{&g_390,(void*)0,&g_390,&l_1110,&g_390},{&g_390,&g_390,&l_1110,&g_390,&g_390},{&l_1110,&l_1110,&g_390,&l_1110,&l_1110},{&l_1110,&g_390,&g_390,&l_1110,&g_390}},{{&g_390,&l_1110,&l_1110,&g_390,&g_390},{(void*)0,&g_390,&g_390,&l_1110,&g_390},{&g_390,(void*)0,&g_390,&g_390,&g_390},{&l_1110,&l_1110,&l_1110,&l_1110,&g_390},{&l_1110,&g_390,&g_390,&l_1110,&g_390}},{{&g_390,&g_390,&g_390,&l_1110,&g_390},{&g_390,&g_390,&l_1110,&g_390,&l_1110},{(void*)0,&l_1110,&g_390,&l_1110,&g_390},{&l_1110,(void*)0,&l_1110,&l_1110,&g_390},{&l_1110,(void*)0,&l_1110,&g_390,&l_1110}},{{(void*)0,&l_1110,&l_1110,(void*)0,(void*)0},{&l_1110,&l_1110,&g_390,&g_390,&l_1110},{&l_1110,&g_390,&g_390,&l_1110,(void*)0},{(void*)0,&l_1110,(void*)0,(void*)0,&l_1110},{&l_1110,(void*)0,&g_390,&l_1110,&g_390}},{{&l_1110,&l_1110,&l_1110,&g_390,(void*)0},{(void*)0,&l_1110,&g_390,(void*)0,&l_1110},{(void*)0,&l_1110,(void*)0,&g_390,&l_1110},{&g_390,&l_1110,&g_390,&l_1110,&l_1110},{(void*)0,&l_1110,&g_390,(void*)0,&g_390}},{{&g_390,(void*)0,&l_1110,&l_1110,&g_390},{(void*)0,&l_1110,&l_1110,&l_1110,&g_390},{(void*)0,&g_390,&l_1110,(void*)0,&l_1110},{&l_1110,&l_1110,&l_1110,&l_1110,&l_1110},{&l_1110,&l_1110,&g_390,&l_1110,&l_1110}},{{(void*)0,(void*)0,&l_1110,(void*)0,(void*)0},{&l_1110,(void*)0,&l_1110,&l_1110,&g_390},{&g_390,&g_390,(void*)0,&l_1110,&l_1110},{&l_1110,&g_390,(void*)0,(void*)0,&l_1110},{&g_390,&l_1110,&l_1110,&l_1110,&l_1110}}}; + struct S1 *l_1112 = &g_1113; + uint8_t ***l_1118[9][9] = {{(void*)0,&g_254,(void*)0,&g_254,&g_254,(void*)0,&g_254,(void*)0,&g_254},{&g_254,(void*)0,(void*)0,&g_254,&g_254,(void*)0,&g_254,&g_254,&g_254},{&g_254,&g_254,(void*)0,&g_254,&g_254,(void*)0,(void*)0,&g_254,&g_254},{(void*)0,&g_254,(void*)0,&g_254,&g_254,(void*)0,&g_254,(void*)0,&g_254},{&g_254,(void*)0,(void*)0,&g_254,&g_254,(void*)0,&g_254,&g_254,&g_254},{&g_254,&g_254,(void*)0,&g_254,&g_254,(void*)0,(void*)0,&g_254,&g_254},{(void*)0,&g_254,(void*)0,&g_254,&g_254,(void*)0,&g_254,(void*)0,&g_254},{&g_254,(void*)0,(void*)0,&g_254,&g_254,(void*)0,&g_254,&g_254,&g_254},{&g_254,&g_254,(void*)0,&g_254,&g_254,(void*)0,(void*)0,&g_254,&g_254}}; + uint8_t ****l_1117 = &l_1118[0][8]; + const int8_t *l_1120 = &g_279; + int16_t *l_1121[8][2] = {{(void*)0,&g_82.f1},{&g_1052,&g_82.f1},{(void*)0,&g_82.f1},{&g_1052,&g_82.f1},{(void*)0,&g_82.f1},{&g_1052,&g_82.f1},{(void*)0,&g_82.f1},{&g_1052,&g_82.f1}}; + uint32_t *l_1123 = &g_18.f0; + int32_t l_1124 = 0xEDBDFF21L; + uint32_t l_1125 = 0UL; + int32_t l_1181 = 0L; + int32_t l_1183 = 0xF8FD7B68L; + int i, j, k; + l_1112 = l_1110; + (*p_16) ^= 0x92C5D770L; + if ((safe_add_func_int32_t_s_s(((l_1116[4][0] = 0UL) , (((l_1126 &= ((((((*l_1123) = (((((&g_254 != ((*l_1117) = &g_254)) , l_1119) == l_1119) , (l_1122 ^= l_1116[1][6])) ^ 0xEF73L)) , (-9L)) <= l_1124) && 0xDAL) , l_1125)) <= 0x1610ADAAL) && 65535UL)), p_14))) + { /* block id: 757 */ +lbl_1158: + (*p_15) = (0x3E96A966L < (*p_16)); + } + else + { /* block id: 759 */ + struct S1 ****l_1131 = &l_1129; + int32_t l_1147 = 0xF6AFA3B1L; + int32_t l_1150 = 0xA4754866L; + int32_t l_1177 = 0L; + int32_t l_1179 = (-4L); + int32_t l_1182[9] = {1L,1L,1L,1L,1L,1L,1L,1L,1L}; + int16_t l_1185 = 0x36BEL; + int i; + if ((safe_lshift_func_int8_t_s_s(p_14, (((*l_1131) = l_1129) == (g_82 , &l_1130))))) + { /* block id: 761 */ + uint32_t l_1135 = 0xD5BBD21CL; + for (g_1109 = 15; (g_1109 > (-10)); g_1109--) + { /* block id: 764 */ + int32_t *l_1134[1][3]; + int i, j; + for (i = 0; i < 1; i++) + { + for (j = 0; j < 3; j++) + l_1134[i][j] = &l_1124; + } + for (g_2 = 0; (g_2 <= 0); g_2 += 1) + { /* block id: 767 */ + return p_16; + } + l_1135--; + } + for (g_802 = 2; (g_802 <= 8); g_802 += 1) + { /* block id: 774 */ + for (g_1113.f9 = 0; (g_1113.f9 <= 2); g_1113.f9 += 1) + { /* block id: 777 */ + uint32_t ***l_1138 = (void*)0; + uint32_t **l_1140 = (void*)0; + uint32_t ***l_1139[7]; + int i; + for (i = 0; i < 7; i++) + l_1139[i] = &l_1140; + g_1141 = &l_1123; + } + for (g_279 = 0; (g_279 <= 8); g_279 += 1) + { /* block id: 782 */ + (*g_220) = p_16; + } + } + for (g_82.f4 = 0; (g_82.f4 == 25); g_82.f4++) + { /* block id: 788 */ + for (g_82.f0 = 0; (g_82.f0 <= 0); g_82.f0 += 1) + { /* block id: 791 */ + int i; + (*g_220) = func_19(p_15, p_15, (*g_390)); + (*g_220) = func_19((*g_220), p_16, (*g_390)); + } + } + } + else + { /* block id: 796 */ + uint32_t **l_1145 = &l_1123; + int32_t l_1146 = (-6L); + int8_t *l_1163[3][5] = {{&g_809[0][0][0],&g_36[3],&g_809[0][0][0],&g_809[0][0][0],&g_36[0]},{&g_279,&g_537[0],&g_36[0],&g_537[0],&g_279},{&g_809[0][0][0],&g_537[0],&g_36[3],&g_279,&g_36[3]}}; + int32_t l_1176[3][1]; + int i, j; + for (i = 0; i < 3; i++) + { + for (j = 0; j < 1; j++) + l_1176[i][j] = 0x96B5594EL; + } + (**l_1129) = (**l_1129); + for (g_279 = 8; (g_279 >= 3); g_279 -= 1) + { /* block id: 800 */ + uint8_t l_1155 = 255UL; + (*p_16) = (((void*)0 != l_1145) | l_1146); + for (g_501 = 0; (g_501 <= 2); g_501 += 1) + { /* block id: 804 */ + int32_t *l_1148 = &g_1109; + int32_t *l_1149 = &g_433; + int32_t *l_1151 = &g_500; + int32_t *l_1152 = &g_238; + int32_t *l_1153 = &g_434; + int32_t *l_1154[9] = {(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0}; + int i; + (*p_16) &= (*p_15); + if (l_1147) + break; + --l_1155; + } + } + for (g_18.f0 = 0; (g_18.f0 <= 0); g_18.f0 += 1) + { /* block id: 812 */ + int32_t *l_1171 = &l_1146; + int32_t *l_1172 = &g_434; + int32_t *l_1173 = (void*)0; + int32_t *l_1174 = (void*)0; + int32_t *l_1175[9][1][2] = {{{&l_1124,&g_238}},{{&l_1147,&g_238}},{{&l_1124,&g_39}},{{&g_39,&l_1124}},{{&g_238,&l_1147}},{{&g_238,&l_1124}},{{&g_39,&g_39}},{{&l_1124,&g_238}},{{&l_1147,&g_238}}}; + int32_t l_1184 = 0xC96082DDL; + uint32_t l_1186[10][9][2] = {{{0x6E9628BBL,18446744073709551615UL},{18446744073709551615UL,0x2C2C3B62L},{18446744073709551608UL,4UL},{2UL,18446744073709551612UL},{0x2C2C3B62L,0x2C2C3B62L},{0UL,1UL},{0x6E9628BBL,18446744073709551607UL},{0x886E6C5AL,0xFCF6436BL},{18446744073709551612UL,0x886E6C5AL}},{{18446744073709551613UL,18446744073709551609UL},{18446744073709551613UL,0x886E6C5AL},{18446744073709551612UL,0xFCF6436BL},{0x886E6C5AL,18446744073709551607UL},{0x6E9628BBL,1UL},{18446744073709551607UL,18446744073709551615UL},{18446744073709551615UL,0x02BC55E2L},{18446744073709551606UL,0xFCF6436BL},{0xFFBA7176L,18446744073709551615UL}},{{0x8232C893L,18446744073709551612UL},{0x886E6C5AL,1UL},{1UL,2UL},{0xFCF6436BL,1UL},{18446744073709551615UL,0x6E9628BBL},{0UL,18446744073709551613UL},{0xFCF6436BL,4UL},{18446744073709551613UL,1UL},{18446744073709551606UL,4UL}},{{0x8232C893L,0xFFBA7176L},{18446744073709551615UL,0xFCF6436BL},{0xDCED0008L,0xFCF6436BL},{18446744073709551615UL,0xFFBA7176L},{0x8232C893L,4UL},{18446744073709551606UL,1UL},{18446744073709551613UL,4UL},{0xFCF6436BL,18446744073709551613UL},{0UL,0x6E9628BBL}},{{18446744073709551615UL,1UL},{0xFCF6436BL,2UL},{1UL,1UL},{0x886E6C5AL,18446744073709551612UL},{0x8232C893L,18446744073709551615UL},{0xFFBA7176L,0xFCF6436BL},{18446744073709551606UL,0x02BC55E2L},{18446744073709551615UL,18446744073709551615UL},{18446744073709551607UL,4UL}},{{0x886E6C5AL,18446744073709551615UL},{18446744073709551613UL,2UL},{0x02BC55E2L,18446744073709551613UL},{18446744073709551615UL,0xB081ED74L},{18446744073709551615UL,18446744073709551613UL},{0x02BC55E2L,2UL},{18446744073709551613UL,18446744073709551615UL},{0x886E6C5AL,4UL},{18446744073709551607UL,18446744073709551615UL}},{{18446744073709551615UL,0x02BC55E2L},{18446744073709551606UL,0xFCF6436BL},{0xFFBA7176L,18446744073709551615UL},{0x8232C893L,18446744073709551612UL},{0x886E6C5AL,1UL},{1UL,2UL},{0xFCF6436BL,1UL},{18446744073709551615UL,18446744073709551606UL},{0xFA7C8EF7L,0UL}},{{4UL,1UL},{0UL,18446744073709551612UL},{18446744073709551608UL,0x02BC55E2L},{18446744073709551615UL,0x9A98F82CL},{18446744073709551609UL,4UL},{18446744073709551609UL,4UL},{18446744073709551609UL,0x9A98F82CL},{18446744073709551615UL,0x02BC55E2L},{18446744073709551608UL,18446744073709551612UL}},{{0UL,1UL},{4UL,0UL},{0xFA7C8EF7L,18446744073709551606UL},{18446744073709551607UL,3UL},{4UL,0xDCED0008L},{3UL,18446744073709551612UL},{1UL,0xA6DAAE01L},{18446744073709551615UL,18446744073709551609UL},{0x9A98F82CL,4UL}},{{18446744073709551608UL,0x6D14230FL},{18446744073709551609UL,18446744073709551609UL},{0x4F5E8A69L,0x02BC55E2L},{1UL,5UL},{0UL,0xDCED0008L},{0x6D14230FL,0UL},{18446744073709551607UL,3UL},{18446744073709551607UL,0UL},{0x6D14230FL,0xDCED0008L}}}; + int i, j, k; + if ((*p_15)) + break; + if (g_391.f5) + goto lbl_1158; + for (g_2 = 0; (g_2 <= 0); g_2 += 1) + { /* block id: 817 */ + struct S0 *l_1170[8] = {&g_508[3][3][4],&g_508[3][3][4],&g_508[3][3][4],&g_508[3][3][4],&g_508[3][3][4],&g_508[3][3][4],&g_508[3][3][4],&g_508[3][3][4]}; + int i, j; + (*p_16) &= (safe_sub_func_uint32_t_u_u(g_499[g_18.f0][g_2], (l_1116[(g_18.f0 + 6)][(g_2 + 6)] , ((p_14 > (l_1120 != l_1163[2][4])) & (safe_sub_func_uint32_t_u_u((safe_div_func_int32_t_s_s(0x03DEC767L, p_14)), (((g_1168 = &g_508[3][3][4]) != l_1170[4]) , 0x42A71327L))))))); + (*p_16) = (p_14 < 0L); + } + l_1186[1][3][0]++; + for (g_5 = 0; (g_5 <= 0); g_5 += 1) + { /* block id: 825 */ + uint16_t l_1189[4] = {0xF7CAL,0xF7CAL,0xF7CAL,0xF7CAL}; + int i; + --l_1189[3]; + } + } + (*p_15) ^= (((*p_16) = l_1178) , g_1192[0][6]); + } + } + } + else + { /* block id: 833 */ + int32_t *l_1193 = (void*)0; + int32_t *l_1194 = &g_876; + int32_t *l_1195[10][4] = {{&g_237,&l_1180[0],&l_1180[0],&l_1180[0]},{&g_237,&l_1180[0],&g_237,&g_238},{&l_1180[0],&l_1180[0],&g_238,&g_238},{&l_1180[0],&l_1180[0],&l_1178,&l_1180[0]},{&l_1180[0],&l_1180[0],&l_1178,&l_1180[0]},{&l_1180[0],&g_237,&g_238,&l_1178},{&l_1180[0],&g_237,&g_237,&g_238},{&l_1180[0],&g_238,&l_1178,&g_237},{&l_1180[0],&l_1178,&l_1180[0],&l_1180[0]},{&g_238,&g_237,&l_1180[0],&l_1180[0]}}; + uint8_t l_1198 = 0UL; + int i, j; + l_1198++; + } + (*p_15) ^= (safe_lshift_func_int16_t_s_s((safe_div_func_uint32_t_u_u(((*l_1205)++), l_1196[2][6][1])), (l_1180[0] = (((+(safe_lshift_func_uint8_t_u_u(((*l_1210)++), ((safe_lshift_func_int16_t_s_s(l_1116[4][0], 6)) , 1UL)))) == 1L) , p_14)))); + if (l_1216) + { /* block id: 840 */ + return p_15; + } + else + { /* block id: 842 */ + uint16_t l_1221[2][9][6] = {{{1UL,0xA6E1L,8UL,65535UL,1UL,0UL},{0UL,65535UL,0UL,1UL,1UL,0UL},{0xA6E1L,0xA6E1L,0xB9C0L,8UL,1UL,0UL},{0xA6E1L,8UL,65535UL,1UL,0UL,0xB9C0L},{0UL,0xA6E1L,65535UL,65535UL,0xA6E1L,0UL},{1UL,65535UL,0xB9C0L,1UL,0xA6E1L,0UL},{1UL,0xA6E1L,0UL,8UL,0UL,0UL},{1UL,8UL,8UL,1UL,1UL,0xB9C0L},{1UL,0xA6E1L,8UL,65535UL,1UL,0UL}},{{0UL,65535UL,0UL,1UL,1UL,0UL},{0xA6E1L,0xA6E1L,0xB9C0L,8UL,1UL,0UL},{0xA6E1L,8UL,65535UL,1UL,0UL,0xB9C0L},{0UL,0xA6E1L,65535UL,65535UL,0xA6E1L,0UL},{1UL,65535UL,0xB9C0L,1UL,0xA6E1L,0UL},{1UL,0xA6E1L,0UL,8UL,0UL,0UL},{1UL,8UL,8UL,1UL,1UL,0xB9C0L},{1UL,0xA6E1L,8UL,65535UL,1UL,0UL},{0UL,65535UL,0UL,1UL,1UL,0UL}}}; + int32_t *l_1234 = (void*)0; + int32_t l_1237 = 0xDB5C3602L; + int32_t l_1242 = 0L; + int32_t l_1247 = 0xF2CC364AL; + int32_t l_1249 = 0xF1E7180FL; + int32_t l_1251 = 0xA149DBF1L; + int32_t l_1252 = 0xFBB6885AL; + int32_t l_1253[5][9][5] = {{{0L,0x84665002L,2L,(-7L),0xBD32F491L},{0xA6FBD2E6L,0L,0xC28A2745L,0xBD32F491L,(-10L)},{(-1L),0L,3L,0L,0L},{0L,0x0ACB57C3L,1L,0xBB327930L,(-9L)},{0xD71E7DEDL,0xA08123F0L,5L,0L,0L},{0x267D6E1AL,0x32F668E8L,0x3E8D01CAL,0xA6FBD2E6L,0x32F668E8L},{(-3L),0x32F668E8L,2L,0L,0xA7E3B020L},{7L,0xA08123F0L,0L,0xF657A079L,0x51757FC2L},{0x98382590L,0x0ACB57C3L,0L,0xD71E7DEDL,0x09C94484L}},{{1L,0L,0xBB327930L,0xF602B128L,0L},{(-9L),0L,0L,(-9L),2L},{0x267D6E1AL,0x84665002L,0x16EEF47BL,7L,0x09C94484L},{(-8L),7L,(-10L),5L,9L},{0xF657A079L,0L,0xA08123F0L,7L,1L},{0L,0x84665002L,0xEDF5817DL,0L,0x267D6E1AL},{0xA08123F0L,0x83309CD8L,0xF602B128L,0L,(-1L)},{(-1L),(-3L),(-10L),0xA7E3B020L,2L},{0xD71E7DEDL,0xA6FBD2E6L,(-6L),0xBD32F491L,(-1L)}},{{0xBB327930L,(-9L),0x770F047CL,1L,0L},{0x09C94484L,0xA08123F0L,(-7L),0x83309CD8L,1L},{0x09C94484L,0x84665002L,0x78A1D5A5L,(-1L),0x83309CD8L},{0xBB327930L,(-1L),0xAED3E08DL,(-8L),0xBD32F491L},{0xD71E7DEDL,0xAED3E08DL,0L,0x84665002L,0xA08123F0L},{(-1L),0xD71E7DEDL,0xAAF60F01L,0x09C94484L,0x09C94484L},{0xA08123F0L,0xBD32F491L,0xA08123F0L,1L,0L},{0L,0x770F047CL,(-3L),0x84665002L,1L},{0xBD32F491L,0L,0x3E8D01CAL,0xD71E7DEDL,0L}},{{0x32DA8A12L,0x0ACB57C3L,(-3L),1L,(-1L)},{(-1L),0xF602B128L,0xA08123F0L,0x83309CD8L,0xF602B128L},{0L,0x267D6E1AL,0xAAF60F01L,0L,7L},{0xAED3E08DL,0x0ACB57C3L,0L,(-7L),0L},{(-9L),0L,0xAED3E08DL,0xA7E3B020L,0x51757FC2L},{0x32F668E8L,(-10L),0x78A1D5A5L,(-1L),0x84665002L},{(-5L),0L,0x074F165CL,(-5L),0L},{0xC28A2745L,0xBFC76879L,(-1L),0L,0xD0F8360AL},{1L,0L,0x32F668E8L,1L,2L}},{{0x074F165CL,0xF602B128L,0L,0L,0x51757FC2L},{0xA08123F0L,7L,0x1C985263L,0x770F047CL,0x1C985263L},{0x32DA8A12L,0x32DA8A12L,(-1L),0xC28A2745L,0xF602B128L},{0xAAF60F01L,1L,0L,1L,0x770F047CL},{(-7L),0x78A1D5A5L,(-10L),0L,0xAAF60F01L},{0L,1L,0x0ACB57C3L,0xA08123F0L,0xAED3E08DL},{3L,0x32DA8A12L,0x074F165CL,0x748F8E4EL,5L},{5L,7L,0xBFC76879L,0xBB327930L,0x748F8E4EL},{0L,0xF602B128L,0L,(-7L),0L}}}; + int8_t *l_1284 = &g_809[1][0][5]; + int i, j, k; + (*p_16) = (!(safe_sub_func_int32_t_s_s((safe_mod_func_uint16_t_u_u((l_1180[1] |= p_14), 1L)), ((l_1196[2][4][3] < (-1L)) >= (&g_365 != ((++l_1221[0][4][4]) , &g_365)))))); +lbl_1285: + for (g_82.f3 = (-28); (g_82.f3 == 18); g_82.f3 = safe_add_func_int8_t_s_s(g_82.f3, 3)) + { /* block id: 848 */ + int16_t l_1235 = (-2L); + int32_t l_1238 = 1L; + int8_t l_1239 = 0x39L; + int32_t l_1241[3][10][2] = {{{0x0F2A4A28L,0L},{0xE6C9B04AL,1L},{0xD502F28DL,(-1L)},{0L,0x674EC8C3L},{0x674EC8C3L,0x674EC8C3L},{0L,(-1L)},{0xD502F28DL,1L},{0xE6C9B04AL,0L},{0x0F2A4A28L,0xE6C9B04AL},{0x9CBF4857L,0xC8A66176L}},{{0x9CBF4857L,0xE6C9B04AL},{0x0F2A4A28L,0L},{0xE6C9B04AL,1L},{0xD502F28DL,(-1L)},{0L,0x674EC8C3L},{0x674EC8C3L,0x674EC8C3L},{0L,(-1L)},{0xD502F28DL,0x9CBF4857L},{1L,0xE6C9B04AL},{(-1L),1L}},{{0x674EC8C3L,0x0F2A4A28L},{0x674EC8C3L,1L},{(-1L),0xE6C9B04AL},{1L,0x9CBF4857L},{0L,0x1D6C64F6L},{0xE6C9B04AL,0xC8A66176L},{0xC8A66176L,0xC8A66176L},{0xE6C9B04AL,0x1D6C64F6L},{0L,0x9CBF4857L},{1L,0xE6C9B04AL}}}; + int32_t l_1259 = 0x4D187B5CL; + int32_t l_1262 = 0x2EE8D66DL; + struct S1 l_1266 = {2,2376,563,217,1949,19,665,112,9,0x0FC184AEL}; + int i, j, k; + } + if ((safe_unary_minus_func_uint32_t_u((p_14 != p_14)))) + { /* block id: 885 */ + int32_t *l_1286 = &l_1247; + if (g_391.f3) + goto lbl_1285; + return p_16; + } + else + { /* block id: 888 */ + (*p_15) = (~((*p_16) = 0L)); + } + } + (*g_220) = l_1287; + return (*g_220); +} + + +/* ------------------------------------------ */ +/* + * reads : g_18 g_542 g_555 g_119 g_1105 + * writes: g_2 g_542 g_555 g_119 g_1105 g_1109 + */ +static int32_t * func_19(int32_t * p_20, int32_t * p_21, struct S1 p_22) +{ /* block id: 736 */ + uint16_t l_1090 = 1UL; + struct S0 l_1091 = {0UL,-9L,65535UL,0xE985L,0xC8L}; + uint32_t *l_1092 = &g_542; + uint32_t *l_1093[1]; + int32_t l_1094 = 0x39EBFE2DL; + uint16_t **l_1095 = &g_601; + int32_t l_1096 = 0x616926E9L; + uint8_t *l_1097 = &g_555[0][0]; + uint8_t *l_1100 = &g_119; + int32_t *l_1101[9][2] = {{(void*)0,&g_433},{&g_434,&g_434},{&g_434,&g_433},{(void*)0,&g_238},{&g_433,&g_238},{(void*)0,&g_433},{&g_434,&g_434},{&g_434,&g_433},{(void*)0,&g_238}}; + uint8_t l_1102[1]; + int i, j; + for (i = 0; i < 1; i++) + l_1093[i] = &g_347; + for (i = 0; i < 1; i++) + l_1102[i] = 255UL; + (*p_20) = (safe_rshift_func_int8_t_s_s(((safe_add_func_uint16_t_u_u(p_22.f0, l_1090)) , p_22.f5), 4)); + (*p_20) = ((l_1091.f1 , p_22.f6) ^ ((*l_1100) ^= (((g_18 , ((p_22.f7 |= (l_1094 = ((*l_1092) &= l_1091.f4))) & ((l_1096 = (l_1095 == l_1095)) && (++(*l_1097))))) || 7UL) != 0xF44BF0BDL))); + l_1102[0]--; + g_1105[8]--; + return p_20; +} + + +/* ------------------------------------------ */ +/* + * reads : g_18.f1 g_2 g_39 g_40 g_18.f3 g_71 g_36 g_82 g_5 g_18.f4 g_18.f0 g_119 g_79 g_195 g_209 g_18.f2 g_222 g_220 g_199 g_208 g_241 g_240 g_254 g_282 g_237 g_280 g_347 g_238 g_365 g_390 g_408 g_391.f4 g_391.f9 g_391 g_501 g_508.f0 g_541 g_508.f4 g_511 g_512 g_434 g_601 g_537 g_499 g_508.f2 g_627 g_251 g_508.f1 g_553 g_628 g_542 g_555 g_433 g_802 g_809 g_876 g_500 g_366 g_279 g_18 + * writes: g_39 g_40 g_71 g_18.f3 g_18.f4 g_36 g_82.f1 g_119 g_82.f4 g_2 g_79 g_195 g_199 g_209 g_5 g_82.f3 g_220 g_222 g_241 g_251 g_282 g_280 g_313 g_237 g_347 g_238 g_365 g_390 g_366 g_408 g_208 g_501 g_500 g_542 g_18.f1 g_434 g_254 g_541 g_672 g_433 g_279 g_825 g_809 g_876 g_540 g_537 g_802 + */ +static struct S1 func_24(uint8_t p_25, int8_t p_26, int32_t * p_27, int32_t p_28) +{ /* block id: 8 */ + uint16_t *l_47 = &g_18.f3; + int16_t l_72 = 0xAD7DL; + int32_t l_74 = 0x16071ECCL; + int32_t l_575[6][3]; + struct S1 l_592 = {0,-1419,958,232,442,9,-5643,122,57,4294967290UL}; + uint16_t **l_700 = &g_601; + int32_t *l_782 = &g_433; + uint32_t l_827 = 4294967295UL; + int32_t l_938 = (-6L); + uint32_t l_957 = 0xD9715F58L; + int8_t *l_1012 = (void*)0; + uint32_t l_1070[1]; + uint32_t l_1081 = 1UL; + int32_t *l_1084 = &l_74; + int i, j; + for (i = 0; i < 6; i++) + { + for (j = 0; j < 3; j++) + l_575[i][j] = 0L; + } + for (i = 0; i < 1; i++) + l_1070[i] = 0xE68764B7L; + if (g_18.f1) + { /* block id: 9 */ + int32_t *l_37 = (void*)0; + int32_t *l_38[4][4][10] = {{{(void*)0,&g_2,&g_2,&g_2,(void*)0,(void*)0,&g_39,&g_39,&g_2,&g_2},{(void*)0,&g_2,&g_2,&g_2,(void*)0,(void*)0,&g_2,(void*)0,&g_39,&g_39},{(void*)0,&g_2,&g_2,(void*)0,&g_39,&g_39,&g_39,&g_2,&g_39,&g_39},{&g_39,&g_2,&g_39,&g_2,&g_2,&g_39,&g_2,&g_39,&g_39,&g_39}},{{&g_2,&g_39,&g_39,&g_2,&g_2,&g_2,&g_39,&g_2,&g_2,&g_2},{&g_39,(void*)0,&g_39,&g_39,&g_2,&g_2,&g_39,&g_2,&g_2,&g_39},{&g_39,&g_39,&g_39,&g_2,&g_39,&g_39,&g_2,&g_2,&g_2,&g_39},{(void*)0,&g_39,&g_2,&g_39,(void*)0,&g_39,&g_39,&g_2,(void*)0,&g_39}},{{&g_2,&g_2,(void*)0,&g_2,(void*)0,&g_39,&g_2,&g_2,&g_39,&g_39},{&g_2,&g_2,&g_2,&g_39,(void*)0,(void*)0,&g_2,&g_2,&g_2,&g_39},{&g_2,&g_39,&g_39,(void*)0,&g_39,(void*)0,(void*)0,&g_39,(void*)0,&g_39},{&g_39,&g_39,&g_39,(void*)0,&g_2,&g_2,&g_2,&g_39,(void*)0,(void*)0}},{{(void*)0,&g_39,&g_2,&g_2,&g_39,&g_2,&g_2,&g_2,&g_39,&g_2},{&g_2,&g_39,&g_39,&g_2,(void*)0,&g_2,(void*)0,(void*)0,&g_39,&g_2},{&g_2,&g_39,&g_2,&g_39,&g_2,&g_39,&g_2,&g_39,(void*)0,(void*)0},{&g_39,(void*)0,&g_39,&g_39,&g_2,(void*)0,&g_39,&g_39,&g_39,&g_39}}}; + int32_t *l_61[2][1]; + uint16_t *l_70 = &g_71; + uint8_t *l_73[2]; + int16_t *l_75 = (void*)0; + int16_t *l_76 = (void*)0; + int16_t *l_77 = &l_72; + uint16_t *l_78 = &g_79; + struct S1 l_603 = {3,-2739,1333,112,1230,-2,4967,47,47,0xB711FC9EL}; + uint8_t **l_675 = &l_73[0]; + int32_t l_695 = 7L; + uint16_t l_749[7][10][3] = {{{0UL,65529UL,0xC0C0L},{65535UL,65529UL,0x94E8L},{0x61EEL,0x8649L,9UL},{0xEF64L,65535UL,0x0753L},{65535UL,0x8FD1L,0x62BAL},{0xDD08L,0x8DCEL,0x4F98L},{65526UL,1UL,65535UL},{0xE257L,0x0394L,0xE257L},{0x0394L,65535UL,65535UL},{0xCE6DL,65535UL,0x05EEL}},{{1UL,0x4888L,1UL},{1UL,0xE257L,1UL},{1UL,1UL,65529UL},{0xCE6DL,1UL,0xE143L},{0x0394L,0x05EEL,65528UL},{0xE257L,0xBEEBL,0xF789L},{65526UL,0x55B4L,65531UL},{0xDD08L,1UL,0xBB19L},{65535UL,0xBB19L,65535UL},{0xEF64L,0xCE6DL,0x9B57L}},{{0x61EEL,65535UL,0x105EL},{65535UL,0xABD0L,0x105EL},{0UL,65526UL,0x9B57L},{0x0C0CL,1UL,65535UL},{0xF789L,0x94E8L,0xBB19L},{0x4F98L,0x0753L,65531UL},{65535UL,0xFC46L,0xF789L},{0xE143L,65528UL,65535UL},{0xBEEBL,1UL,0x0C0CL},{0xBB19L,0x4888L,1UL}},{{1UL,9UL,1UL},{65535UL,65529UL,0xDF24L},{0x55B4L,9UL,65531UL},{0x1339L,0x4888L,1UL},{65535UL,1UL,0x33CFL},{65531UL,65535UL,9UL},{65526UL,0x105EL,65535UL},{0xDF24L,65529UL,0xBEEBL},{0xFC46L,0x4979L,65529UL},{1UL,1UL,0xEF64L}},{{1UL,0xE143L,0x4979L},{1UL,0x94E8L,0x61EEL},{1UL,0xEB09L,1UL},{1UL,0x2E04L,0x62BAL},{1UL,65535UL,0x8649L},{0xFC46L,1UL,0x105EL},{0xDF24L,65535UL,0x1339L},{65526UL,0xCE6DL,1UL},{65531UL,65531UL,65535UL},{65535UL,0xCBF3L,1UL}},{{0x1339L,0x8DCEL,0x1123L},{0x55B4L,0x33CFL,65535UL},{65535UL,0x1339L,0x1123L},{1UL,65535UL,1UL},{0xBB19L,5UL,65535UL},{0xBEEBL,0x9B57L,1UL},{0x0C0CL,0xFC46L,0x1339L},{0xEB09L,1UL,0x105EL},{65535UL,0UL,0x8649L},{0x4888L,0x8DCEL,65535UL}},{{0xDD08L,65535UL,1UL},{0x4F98L,65535UL,0x55B4L},{65529UL,65535UL,0x62BAL},{65535UL,65535UL,0xFC46L},{0x105EL,0x8DCEL,0UL},{65526UL,9UL,65528UL},{0UL,1UL,0x05EEL},{65535UL,0xCBF3L,1UL},{0x8649L,65535UL,0x8649L},{65535UL,65531UL,0xCE6DL}}}; + int32_t ** const *l_786[2][8][5] = {{{(void*)0,(void*)0,&g_365,(void*)0,&g_365},{&g_365,&g_365,&g_365,&g_365,&g_365},{(void*)0,&g_365,(void*)0,&g_365,(void*)0},{&g_365,&g_365,&g_365,&g_365,(void*)0},{&g_365,(void*)0,(void*)0,&g_365,(void*)0},{(void*)0,&g_365,&g_365,(void*)0,&g_365},{&g_365,&g_365,(void*)0,&g_365,&g_365},{&g_365,&g_365,&g_365,(void*)0,&g_365}},{{&g_365,&g_365,&g_365,&g_365,&g_365},{&g_365,&g_365,(void*)0,&g_365,&g_365},{&g_365,&g_365,&g_365,&g_365,&g_365},{&g_365,&g_365,&g_365,&g_365,&g_365},{&g_365,&g_365,&g_365,(void*)0,&g_365},{(void*)0,&g_365,(void*)0,&g_365,&g_365},{&g_365,&g_365,(void*)0,&g_365,&g_365},{&g_365,&g_365,&g_365,&g_365,(void*)0}}}; + struct S1 **l_818 = &g_390; + int32_t l_860 = 8L; + int32_t l_892 = 0x202F9FF8L; + int i, j, k; + for (i = 0; i < 2; i++) + { + for (j = 0; j < 1; j++) + l_61[i][j] = &g_39; + } + for (i = 0; i < 2; i++) + l_73[i] = &g_18.f4; +lbl_673: + g_40 ^= (g_39 &= (*p_27)); + if ((func_41(g_18.f1, l_47, (safe_sub_func_int32_t_s_s((safe_mul_func_uint8_t_u_u((func_52(func_56(l_61[1][0], ((*l_77) = ((safe_sub_func_uint32_t_u_u(0xF8A1C32FL, ((l_74 |= (safe_mul_func_uint16_t_u_u(((*l_47) = (safe_sub_func_int16_t_s_s((g_40 , (safe_rshift_func_int8_t_s_s((g_18.f3 , ((*p_27) <= (*p_27))), 5))), (((*l_70) |= 65535UL) || p_25)))), l_72))) || p_25))) , (-2L))), l_78, &g_36[2]), g_82, l_77) && g_541), g_508[3][3][4].f4)), l_575[0][0])), g_391.f9, (*g_511)) != p_25)) + { /* block id: 367 */ + int32_t l_587 = 0xAFFEE3ACL; + uint8_t l_588 = 0xFBL; + int32_t l_602 = 0xF8DD99D7L; + if ((g_391.f1 == (*p_27))) + { /* block id: 368 */ + uint16_t l_584 = 65535UL; + struct S1 l_591[9] = {{3,2063,1226,210,751,33,2362,168,69,0x04BFF210L},{3,2063,1226,210,751,33,2362,168,69,0x04BFF210L},{3,2063,1226,210,751,33,2362,168,69,0x04BFF210L},{3,2063,1226,210,751,33,2362,168,69,0x04BFF210L},{3,2063,1226,210,751,33,2362,168,69,0x04BFF210L},{3,2063,1226,210,751,33,2362,168,69,0x04BFF210L},{3,2063,1226,210,751,33,2362,168,69,0x04BFF210L},{3,2063,1226,210,751,33,2362,168,69,0x04BFF210L},{3,2063,1226,210,751,33,2362,168,69,0x04BFF210L}}; + int i; + for (g_542 = 0; (g_542 <= 2); g_542 += 1) + { /* block id: 371 */ + uint16_t l_581 = 0UL; + l_581++; + l_584--; + } + for (g_18.f1 = 0; (g_18.f1 <= 7); g_18.f1 += 1) + { /* block id: 377 */ + l_588--; + } + for (g_238 = 0; (g_238 <= 2); g_238 += 1) + { /* block id: 382 */ + for (g_82.f1 = 0; (g_82.f1 <= 1); g_82.f1 += 1) + { /* block id: 385 */ + return l_591[3]; + } + for (g_434 = 0; (g_434 <= 2); g_434 += 1) + { /* block id: 390 */ + uint16_t **l_593 = (void*)0; + uint16_t **l_594 = &l_70; + struct S1 l_595 = {2,-2250,1129,1,2028,29,5333,169,16,6UL}; + int i, j; + l_575[g_434][g_434] = (((*l_594) = (l_575[(g_434 + 2)][g_238] , (l_592 , &g_71))) == (void*)0); + return l_595; + } + } + } + else + { /* block id: 396 */ + int32_t *l_596 = (void*)0; + l_602 |= func_41((((((void*)0 == l_596) , (safe_mod_func_int16_t_s_s((l_588 , (((((*l_78) = p_26) , l_47) != l_47) <= func_41((safe_add_func_uint32_t_u_u(func_41(p_26, g_601, l_587, l_72, (*g_511)), (*p_27))), &g_282, l_588, p_28, (*g_511)))), p_28))) , 0xADL) , g_434), l_75, p_26, l_592.f2, (*g_511)); + } + } + else + { /* block id: 400 */ + struct S0 *l_604 = (void*)0; + if ((*p_27)) + { /* block id: 401 */ + return l_603; + } + else + { /* block id: 403 */ + struct S0 **l_605 = &l_604; + (*l_605) = l_604; + return l_603; + } + } + if ((l_575[2][2] |= func_87(l_72))) + { /* block id: 409 */ + uint16_t l_631[6]; + uint32_t l_637 = 0x181F7B27L; + int32_t l_670 = 0xD9A9CC58L; + uint32_t l_676[1]; + struct S1 l_679[7][10][2] = {{{{2,-1595,443,51,912,38,-5277,33,28,1UL},{2,1593,542,200,804,28,4679,20,13,0x3BD0E54FL}},{{3,413,618,36,589,4,634,43,83,0xAF2EF906L},{1,-988,1433,245,1849,41,1390,72,82,0xE0E5DFA6L}},{{0,-523,834,104,710,-13,-2819,22,16,0UL},{3,-384,30,246,951,38,-5520,66,28,0UL}},{{0,2383,255,170,10,-40,4091,149,35,4294967295UL},{3,30,1123,48,463,-38,-3243,95,17,0x1E3FA1E9L}},{{0,-1550,67,243,803,1,3103,136,36,0x608032FFL},{0,-2441,1229,9,874,-6,2068,127,59,0xAFA15E54L}},{{0,184,615,146,238,40,-4905,150,1,0x5702A702L},{0,-2441,1229,9,874,-6,2068,127,59,0xAFA15E54L}},{{0,-1550,67,243,803,1,3103,136,36,0x608032FFL},{3,30,1123,48,463,-38,-3243,95,17,0x1E3FA1E9L}},{{0,2383,255,170,10,-40,4091,149,35,4294967295UL},{3,-384,30,246,951,38,-5520,66,28,0UL}},{{0,-523,834,104,710,-13,-2819,22,16,0UL},{1,-988,1433,245,1849,41,1390,72,82,0xE0E5DFA6L}},{{3,413,618,36,589,4,634,43,83,0xAF2EF906L},{2,1593,542,200,804,28,4679,20,13,0x3BD0E54FL}}},{{{2,-1595,443,51,912,38,-5277,33,28,1UL},{1,50,448,210,1100,13,348,78,35,0x1D982557L}},{{1,-2592,900,153,1735,-9,5150,41,17,0x0C603FAEL},{0,-1550,67,243,803,1,3103,136,36,0x608032FFL}},{{0,575,770,106,328,22,-2648,35,12,0x609DEBEDL},{0,-1153,207,214,16,-29,-1669,166,73,0xB9229B32L}},{{1,-988,1433,245,1849,41,1390,72,82,0xE0E5DFA6L},{0,184,615,146,238,40,-4905,150,1,0x5702A702L}},{{0,-1469,733,249,1993,-41,-3293,72,22,0UL},{0,575,770,106,328,22,-2648,35,12,0x609DEBEDL}},{{1,269,1195,186,100,-25,5627,56,53,0x7B86FED5L},{1,269,1195,186,100,-25,5627,56,53,0x7B86FED5L}},{{3,-806,807,85,912,-19,-4175,66,12,4294967295UL},{0,-2387,643,32,290,-16,1613,1,33,4294967295UL}},{{1,50,448,210,1100,13,348,78,35,0x1D982557L},{2,-737,1062,60,555,-8,-4603,152,31,0x138A5C71L}},{{2,-1165,326,103,1319,-1,1999,128,11,0xA99088DAL},{1,37,162,116,1092,11,530,45,61,1UL}},{{2,1593,542,200,804,28,4679,20,13,0x3BD0E54FL},{2,-1165,326,103,1319,-1,1999,128,11,0xA99088DAL}}},{{{2,-134,1348,152,1332,-22,-3446,85,60,0x6EEEC6EFL},{0,101,1325,216,1687,-7,3065,171,67,4294967293UL}},{{2,-134,1348,152,1332,-22,-3446,85,60,0x6EEEC6EFL},{2,-1165,326,103,1319,-1,1999,128,11,0xA99088DAL}},{{2,1593,542,200,804,28,4679,20,13,0x3BD0E54FL},{1,37,162,116,1092,11,530,45,61,1UL}},{{2,-1165,326,103,1319,-1,1999,128,11,0xA99088DAL},{2,-737,1062,60,555,-8,-4603,152,31,0x138A5C71L}},{{1,50,448,210,1100,13,348,78,35,0x1D982557L},{0,-2387,643,32,290,-16,1613,1,33,4294967295UL}},{{3,-806,807,85,912,-19,-4175,66,12,4294967295UL},{1,269,1195,186,100,-25,5627,56,53,0x7B86FED5L}},{{1,269,1195,186,100,-25,5627,56,53,0x7B86FED5L},{0,575,770,106,328,22,-2648,35,12,0x609DEBEDL}},{{0,-1469,733,249,1993,-41,-3293,72,22,0UL},{0,184,615,146,238,40,-4905,150,1,0x5702A702L}},{{1,-988,1433,245,1849,41,1390,72,82,0xE0E5DFA6L},{0,-1153,207,214,16,-29,-1669,166,73,0xB9229B32L}},{{0,575,770,106,328,22,-2648,35,12,0x609DEBEDL},{0,-1550,67,243,803,1,3103,136,36,0x608032FFL}}},{{{1,-2592,900,153,1735,-9,5150,41,17,0x0C603FAEL},{1,50,448,210,1100,13,348,78,35,0x1D982557L}},{{2,-1595,443,51,912,38,-5277,33,28,1UL},{2,1593,542,200,804,28,4679,20,13,0x3BD0E54FL}},{{3,413,618,36,589,4,634,43,83,0xAF2EF906L},{1,-988,1433,245,1849,41,1390,72,82,0xE0E5DFA6L}},{{0,-523,834,104,710,-13,-2819,22,16,0UL},{3,-384,30,246,951,38,-5520,66,28,0UL}},{{0,2383,255,170,10,-40,4091,149,35,4294967295UL},{3,30,1123,48,463,-38,-3243,95,17,0x1E3FA1E9L}},{{0,-1469,733,249,1993,-41,-3293,72,22,0UL},{2,-134,1348,152,1332,-22,-3446,85,60,0x6EEEC6EFL}},{{0,575,770,106,328,22,-2648,35,12,0x609DEBEDL},{2,-134,1348,152,1332,-22,-3446,85,60,0x6EEEC6EFL}},{{0,-1469,733,249,1993,-41,-3293,72,22,0UL},{2,1593,542,200,804,28,4679,20,13,0x3BD0E54FL}},{{2,-737,1062,60,555,-8,-4603,152,31,0x138A5C71L},{2,-1165,326,103,1319,-1,1999,128,11,0xA99088DAL}},{{3,-384,30,246,951,38,-5520,66,28,0UL},{1,50,448,210,1100,13,348,78,35,0x1D982557L}}},{{{3,-2429,38,9,1431,32,2622,125,11,0x26C6F8C5L},{3,-806,807,85,912,-19,-4175,66,12,4294967295UL}},{{0,2253,916,153,1215,-7,3059,88,8,4294967294UL},{1,269,1195,186,100,-25,5627,56,53,0x7B86FED5L}},{{3,30,1123,48,463,-38,-3243,95,17,0x1E3FA1E9L},{0,-1469,733,249,1993,-41,-3293,72,22,0UL}},{{1,-2592,900,153,1735,-9,5150,41,17,0x0C603FAEL},{1,-988,1433,245,1849,41,1390,72,82,0xE0E5DFA6L}},{{1,50,448,210,1100,13,348,78,35,0x1D982557L},{0,575,770,106,328,22,-2648,35,12,0x609DEBEDL}},{{3,2284,1046,224,1326,26,5594,127,73,1UL},{1,-2592,900,153,1735,-9,5150,41,17,0x0C603FAEL}},{{2,-1595,443,51,912,38,-5277,33,28,1UL},{2,-1595,443,51,912,38,-5277,33,28,1UL}},{{0,-1153,207,214,16,-29,-1669,166,73,0xB9229B32L},{3,413,618,36,589,4,634,43,83,0xAF2EF906L}},{{1,269,1195,186,100,-25,5627,56,53,0x7B86FED5L},{0,-523,834,104,710,-13,-2819,22,16,0UL}},{{0,-1550,67,243,803,1,3103,136,36,0x608032FFL},{0,2383,255,170,10,-40,4091,149,35,4294967295UL}}},{{{3,-806,807,85,912,-19,-4175,66,12,4294967295UL},{0,-1550,67,243,803,1,3103,136,36,0x608032FFL}},{{2,2884,1172,138,389,39,-2135,34,51,4294967293UL},{0,184,615,146,238,40,-4905,150,1,0x5702A702L}},{{2,2884,1172,138,389,39,-2135,34,51,4294967293UL},{0,-1550,67,243,803,1,3103,136,36,0x608032FFL}},{{3,-806,807,85,912,-19,-4175,66,12,4294967295UL},{0,2383,255,170,10,-40,4091,149,35,4294967295UL}},{{0,-1550,67,243,803,1,3103,136,36,0x608032FFL},{0,-523,834,104,710,-13,-2819,22,16,0UL}},{{1,269,1195,186,100,-25,5627,56,53,0x7B86FED5L},{3,413,618,36,589,4,634,43,83,0xAF2EF906L}},{{0,-1153,207,214,16,-29,-1669,166,73,0xB9229B32L},{2,-1595,443,51,912,38,-5277,33,28,1UL}},{{2,-1595,443,51,912,38,-5277,33,28,1UL},{1,-2592,900,153,1735,-9,5150,41,17,0x0C603FAEL}},{{3,2284,1046,224,1326,26,5594,127,73,1UL},{0,575,770,106,328,22,-2648,35,12,0x609DEBEDL}},{{1,50,448,210,1100,13,348,78,35,0x1D982557L},{1,-988,1433,245,1849,41,1390,72,82,0xE0E5DFA6L}}},{{{1,-2592,900,153,1735,-9,5150,41,17,0x0C603FAEL},{0,-1469,733,249,1993,-41,-3293,72,22,0UL}},{{3,30,1123,48,463,-38,-3243,95,17,0x1E3FA1E9L},{1,269,1195,186,100,-25,5627,56,53,0x7B86FED5L}},{{0,2253,916,153,1215,-7,3059,88,8,4294967294UL},{3,-806,807,85,912,-19,-4175,66,12,4294967295UL}},{{3,-2429,38,9,1431,32,2622,125,11,0x26C6F8C5L},{1,50,448,210,1100,13,348,78,35,0x1D982557L}},{{3,-384,30,246,951,38,-5520,66,28,0UL},{2,-1165,326,103,1319,-1,1999,128,11,0xA99088DAL}},{{2,-737,1062,60,555,-8,-4603,152,31,0x138A5C71L},{2,1593,542,200,804,28,4679,20,13,0x3BD0E54FL}},{{0,-1469,733,249,1993,-41,-3293,72,22,0UL},{2,-134,1348,152,1332,-22,-3446,85,60,0x6EEEC6EFL}},{{0,575,770,106,328,22,-2648,35,12,0x609DEBEDL},{2,-134,1348,152,1332,-22,-3446,85,60,0x6EEEC6EFL}},{{0,-1469,733,249,1993,-41,-3293,72,22,0UL},{2,1593,542,200,804,28,4679,20,13,0x3BD0E54FL}},{{2,-737,1062,60,555,-8,-4603,152,31,0x138A5C71L},{2,-1165,326,103,1319,-1,1999,128,11,0xA99088DAL}}}}; + uint32_t l_708 = 0x1807D42BL; + int8_t *l_717 = &g_537[0]; + const int16_t l_725 = 0x512FL; + int32_t **l_738 = &g_199; + int i, j, k; + for (i = 0; i < 6; i++) + l_631[i] = 65526UL; + for (i = 0; i < 1; i++) + l_676[i] = 0x17292FF8L; + for (p_28 = 0; (p_28 > (-30)); p_28--) + { /* block id: 412 */ + int32_t * const l_624 = &g_541; + int32_t * const *l_623 = &l_624; + int32_t l_630 = 1L; + if ((*p_27)) + { /* block id: 413 */ + int8_t l_626 = 0x9FL; + for (p_26 = 0; (p_26 >= 0); p_26 -= 1) + { /* block id: 416 */ + int32_t * const **l_625 = &l_623; + uint32_t *l_629 = &g_347; + uint8_t ***l_634 = &g_254; + int i; + (*p_27) = ((l_575[0][0] &= ((safe_rshift_func_uint16_t_u_s(g_537[p_26], 14)) != (safe_div_func_int8_t_s_s((l_592.f4 || (((safe_unary_minus_func_uint8_t_u(255UL)) && ((safe_mod_func_uint32_t_u_u(((*l_629) = (func_87(((safe_lshift_func_uint8_t_u_u(((safe_add_func_uint8_t_u_u(l_592.f9, (g_499[2][0] != ((safe_mul_func_uint16_t_u_u(((func_87(g_537[p_26]) & ((((*l_625) = l_623) != ((g_508[3][3][4].f2 && l_626) , g_627[3][7][1])) <= 5L)) <= 0x6DC3L), p_28)) <= 0x75C34126L)))) <= g_82.f0), p_25)) ^ 0UL)) , g_251)), g_537[p_26])) != p_26)) < 0xC6L)), g_391.f7)))) & 3L); + if (l_630) + break; + ++l_631[5]; + (*l_634) = &l_73[1]; + } + for (g_82.f3 = (-4); (g_82.f3 <= 2); ++g_82.f3) + { /* block id: 427 */ + uint32_t *l_658 = (void*)0; + uint32_t *l_659 = &l_592.f9; + uint32_t *l_662 = &g_251; + int32_t l_667 = (-8L); + int8_t *l_668 = &g_36[3]; + int16_t l_669 = (-1L); + int32_t **l_671 = (void*)0; + if (l_637) + break; + g_237 |= ((l_626 , (safe_rshift_func_int16_t_s_s(((((safe_mul_func_uint16_t_u_u((safe_add_func_uint32_t_u_u((p_26 ^ p_25), (l_630 = ((l_670 = (safe_rshift_func_uint8_t_u_s(((((*p_27) = (safe_add_func_int32_t_s_s(l_630, (l_592.f1 |= (safe_rshift_func_int16_t_s_s((0x8601L & ((safe_mod_func_uint8_t_u_u(g_508[3][3][4].f1, ((*l_668) = ((((*l_662) = ((safe_mul_func_uint16_t_u_u((((*l_77) = (g_82 , (func_41(((*l_624) = (safe_rshift_func_int8_t_s_u((safe_mod_func_uint32_t_u_u(((*l_662) = (--(*l_659))), (((*p_27) = (safe_rshift_func_int8_t_s_u((((safe_rshift_func_int16_t_s_s(p_26, 0)) && p_26) | p_28), p_26))) || g_222))), p_26))), l_70, l_667, p_25, (*g_511)) ^ p_25))) | l_575[0][0]), 0UL)) <= 0x419FL)) | l_631[5]) & l_592.f8)))) , l_669)), l_592.f7)))))) ^ (-1L)) != 0x087E7087L), 2))) < 8L)))), p_26)) , l_626) | 0x01F99D9AL) >= 4294967289UL), 15))) | p_28); + g_672[3] = ((*g_220) = (*g_220)); + } + (*g_220) = &l_575[2][0]; + for (g_433 = 0; (g_433 <= 0); g_433 += 1) + { /* block id: 447 */ + int i, j; + if (g_18.f4) + goto lbl_673; + if ((*p_27)) + break; + (*g_199) &= l_626; + } + } + else + { /* block id: 452 */ + uint8_t ***l_674[8] = {(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0}; + int i; + l_675 = g_254; + for (g_82.f1 = 5; (g_82.f1 >= 1); g_82.f1 -= 1) + { /* block id: 456 */ + --l_676[0]; + } + (*g_220) = (*g_220); + (*p_27) &= 0L; + } + if (l_676[0]) + continue; + for (g_251 = 0; (g_251 <= 1); g_251 += 1) + { /* block id: 465 */ + return (*g_390); + } + return l_679[5][2][1]; + } + (*g_220) = (*g_220); + (*g_220) = (void*)0; + if (l_592.f6) + { /* block id: 472 */ + uint32_t l_689 = 0xD04695A0L; + int32_t l_692 = (-1L); + int32_t l_718 = (-2L); + struct S1 **l_728 = &g_390; + struct S1 ***l_729 = &l_728; + int8_t ** const l_734 = &g_512; + for (g_282 = (-24); (g_282 <= 29); g_282 = safe_add_func_int8_t_s_s(g_282, 7)) + { /* block id: 475 */ + const int32_t * const l_682 = &g_238; + const int32_t **l_683 = (void*)0; + const int32_t **l_684 = (void*)0; + const int32_t *l_686 = &g_238; + const int32_t **l_685 = &l_686; + int32_t l_701 = (-1L); + uint32_t *l_711 = &g_347; + (*l_685) = l_682; + if ((safe_mod_func_uint8_t_u_u(l_689, g_119))) + { /* block id: 477 */ + uint16_t l_702[6][8][5] = {{{0x7923L,8UL,0xC85DL,1UL,0x092FL},{65535UL,0x6D71L,65528UL,0x0282L,0x79AEL},{0x2CD4L,1UL,65535UL,0xE226L,0x6CCCL},{0x7923L,0x2CD4L,65535UL,1UL,0xBFC3L},{9UL,0x7590L,6UL,65535UL,7UL},{65528UL,65535UL,65535UL,0xBFC3L,0x299FL},{2UL,1UL,8UL,0x6CCCL,1UL},{65526UL,0xBE31L,65535UL,0x54AEL,1UL}},{{0x0282L,0xEE59L,65530UL,65531UL,0x299FL},{65535UL,65531UL,65532UL,65535UL,7UL},{0x584BL,0xAED2L,65532UL,6UL,0xBFC3L},{0x6CCCL,65527UL,0x7074L,0x295DL,6UL},{0UL,0x0282L,0xD366L,0xB7C5L,1UL},{0x7074L,0xBE31L,0xC85DL,0xC85DL,0xBE31L},{7UL,6UL,1UL,0x295DL,65532UL},{0x820AL,1UL,9UL,1UL,0xB7C5L}},{{0x299FL,0x7590L,0x81A7L,65532UL,0x820AL},{0x820AL,0x9253L,0x54AEL,65531UL,0x77B6L},{7UL,65535UL,65535UL,2UL,0x295DL},{0x7074L,0x6CCCL,65530UL,65532UL,0x7590L},{0UL,0x348CL,0x54AEL,0xBFC3L,0xEE59L},{0x6CCCL,0x12C3L,1UL,8UL,65528UL},{0x584BL,0x820AL,65526UL,65535UL,0x3B03L},{0xAED2L,65535UL,0x54AEL,0x3B03L,3UL}},{{0x3B03L,0x0282L,9UL,0xBFC3L,0x6CCCL},{65530UL,0x0282L,0xEE59L,65530UL,65531UL},{65535UL,65535UL,3UL,0xAED2L,0xC85DL},{0UL,0xB7C5L,1UL,0x820AL,0xB585L},{65535UL,1UL,65532UL,1UL,0x12C3L},{0x0282L,65535UL,65531UL,65528UL,0x9253L},{7UL,0x12C3L,65535UL,0xBFC3L,0xBFC3L},{0x5DE7L,8UL,0x5DE7L,0x81A7L,65535UL}},{{0x0282L,0x5DE7L,0x43CAL,8UL,0x77B6L},{0xBFC3L,65535UL,0x2CD4L,65535UL,1UL},{65531UL,0x7590L,0x43CAL,0x77B6L,0xBE31L},{65535UL,0x299FL,0x5DE7L,0x12C3L,0x7074L},{65528UL,9UL,65535UL,65535UL,0x7590L},{0x81A7L,0x3B03L,65531UL,65534UL,0xBE31L},{0xAED2L,65534UL,65532UL,0x0282L,0x348CL},{1UL,2UL,1UL,65535UL,0x9DBCL}},{{0x584BL,0x295DL,3UL,65532UL,65535UL},{1UL,0x81A7L,0xEE59L,65531UL,0x6D71L},{0x54AEL,0x348CL,0UL,65531UL,0x348CL},{65535UL,0x9253L,0x79AEL,0x092FL,0x299FL},{65530UL,0x7923L,65527UL,0x9253L,7UL},{0x5DE7L,0x54AEL,0x9DBCL,9UL,65530UL},{65535UL,65535UL,1UL,1UL,65532UL},{65535UL,1UL,0xBEBFL,0x81A7L,0x092FL}}}; + int32_t l_707 = 0x15AB18EFL; + int i, j, k; + (*p_27) &= l_592.f9; + l_702[2][2][1] = (l_692 = ((*p_27) = ((safe_lshift_func_int16_t_s_u((((--(*l_70)) , (l_695 || ((*l_47) = g_36[7]))) != ((safe_lshift_func_int16_t_s_u((safe_add_func_uint16_t_u_u(l_631[5], 0x6158L)), 11)) & ((l_700 = &l_70) == (void*)0))), g_553)) , (l_701 &= (*p_27))))); + l_679[5][2][1].f1 = ((((safe_mul_func_uint8_t_u_u(g_241, l_670)) , ((*g_390) , (((l_702[2][2][1] > (!((((safe_mul_func_int16_t_s_s((l_679[5][2][1].f0 , p_25), l_702[2][2][1])) | func_41(((void*)0 != &l_692), &l_631[5], l_707, l_708, (*g_511))) , 2UL) ^ 0x3BL))) , g_238) == p_25))) ^ (-7L)) && 0x63B1L); + } + else + { /* block id: 487 */ + struct S1 l_709 = {2,-2303,1271,97,843,34,-1511,148,89,8UL}; + uint8_t ***l_710 = &l_675; + p_27 = (l_709 , (void*)0); + (*l_710) = g_254; + l_692 = (l_689 < (0x6EC1D1A8L < ((void*)0 == l_711))); + return l_709; + } + for (p_26 = 8; (p_26 <= 15); p_26 = safe_add_func_uint16_t_u_u(p_26, 9)) + { /* block id: 495 */ + uint16_t l_716 = 65531UL; + l_718 ^= (((l_637 || ((((safe_rshift_func_uint8_t_u_u((0UL ^ (((*l_711) |= func_41((*g_628), &g_79, l_716, l_692, l_717)) < (p_25 ^ p_25))), p_25)) <= p_28) ^ (*l_682)) < p_28)) & p_28) && l_689); + if (l_592.f4) + goto lbl_1085; + (*p_27) = (safe_mul_func_int8_t_s_s(((*g_390) , (p_26 , ((p_25 <= (g_542 < ((safe_add_func_uint16_t_u_u(((*l_70)--), p_26)) ^ (l_701 = (*p_27))))) , (((l_725 == ((((*p_27) ^ (safe_sub_func_int8_t_s_s(l_692, l_670))) <= l_679[5][2][1].f5) != l_631[5])) < g_5) ^ l_689)))), 0xD5L)); + if (l_670) + continue; + } + } + (*l_729) = l_728; + (*p_27) = (((*l_77) = (safe_sub_func_int16_t_s_s((safe_mod_func_int8_t_s_s((p_28 >= (l_734 != &g_512)), p_28)), (0x7C6050E9L ^ l_592.f5)))) | 65535UL); + } + else + { /* block id: 507 */ + int32_t ***l_737[4][1][1] = {{{&g_220}},{{(void*)0}},{{&g_220}},{{(void*)0}}}; + int i, j, k; + (*p_27) = ((*p_27) , (p_28 != ((*p_27) , ((l_738 = &p_27) == (void*)0)))); + } + } + else + { /* block id: 511 */ + int16_t l_743 = 1L; + uint16_t **l_746 = (void*)0; + uint16_t ***l_747 = &l_700; + int32_t l_748[10] = {0xB2FC27D5L,3L,3L,0xB2FC27D5L,3L,3L,0xB2FC27D5L,3L,3L,0xB2FC27D5L}; + int32_t *l_761 = (void*)0; + int32_t ***l_785 = (void*)0; + struct S0 l_826 = {0x59DC1ED7L,0x78C5L,1UL,0x2F09L,1UL}; + uint32_t l_833[9] = {18446744073709551614UL,0x2696DEC0L,18446744073709551614UL,18446744073709551614UL,0x2696DEC0L,18446744073709551614UL,18446744073709551614UL,0x2696DEC0L,18446744073709551614UL}; + int i; + (*g_220) = (l_592 , (*g_220)); + if (((*p_27) | (safe_mul_func_int16_t_s_s((safe_sub_func_int16_t_s_s(((l_743 == 0x9BDFL) , p_26), (((safe_sub_func_uint16_t_u_u((p_25 >= l_592.f5), ((*l_47) |= g_282))) , &l_78) != ((*l_747) = l_746)))), 0x5BEBL)))) + { /* block id: 515 */ + int8_t *l_778 = &g_36[3]; + ++l_749[1][8][2]; + (*g_220) = (p_28 , (*g_220)); + for (g_280 = (-17); (g_280 == (-28)); g_280 = safe_sub_func_uint8_t_u_u(g_280, 9)) + { /* block id: 520 */ + return (*g_390); + } + for (l_592.f9 = 0; (l_592.f9 < 58); l_592.f9 = safe_add_func_int8_t_s_s(l_592.f9, 8)) + { /* block id: 525 */ + uint8_t l_777 = 1UL; + int32_t * const l_779 = (void*)0; + int32_t **l_780 = (void*)0; + int32_t **l_781 = &l_38[3][2][7]; + for (g_82.f4 = 0; (g_82.f4 != 34); ++g_82.f4) + { /* block id: 528 */ + uint8_t l_758 = 0x64L; + uint32_t *l_776[6] = {&g_391.f9,&g_391.f9,&g_391.f9,&g_391.f9,&g_391.f9,&g_391.f9}; + int i; + --l_758; + l_761 = p_27; + (*p_27) = func_41(p_26, &l_749[1][8][2], p_28, ((safe_rshift_func_int8_t_s_s((safe_div_func_uint32_t_u_u((safe_add_func_int8_t_s_s(func_41((l_592.f3 == 2UL), &g_71, (g_555[0][0] , (safe_mod_func_int16_t_s_s(((((((((safe_mod_func_uint32_t_u_u((safe_mod_func_uint16_t_u_u(((*l_70) = ((*l_761) == ((safe_lshift_func_int16_t_s_s(((l_74 ^= g_391.f9) > g_408), 2)) , g_433))), p_28)), (*p_27))) | 0L) < p_26) , p_25) == p_26) , l_758) == 248UL) != 0x274DL), l_777))), p_28, (*g_511)), 0x6DL)), 0x4355FD78L)), l_758)) & l_592.f4), l_778); + } + (*l_781) = l_779; + } + } + else + { /* block id: 537 */ + uint32_t l_793[5]; + uint8_t l_817[8]; + int32_t l_830 = 7L; + int32_t l_831 = 0L; + int32_t l_894 = 0xF408C4C0L; + uint16_t l_895 = 0UL; + int i; + for (i = 0; i < 5; i++) + l_793[i] = 0x154D16D8L; + for (i = 0; i < 8; i++) + l_817[i] = 9UL; + l_782 = p_27; + if ((g_508[3][3][4].f4 >= (safe_lshift_func_int8_t_s_s((l_785 != l_786[0][1][4]), 2)))) + { /* block id: 539 */ + uint8_t l_789 = 0x62L; + int32_t l_792[9][3][3] = {{{0x1206CF87L,0xD4342978L,0x56D0F252L},{0x1206CF87L,0L,1L},{0xB6B593C1L,0xE970D550L,0L}},{{1L,0xFA8A9F6DL,1L},{0x05365D73L,(-4L),0x56D0F252L},{0xD7896F25L,(-4L),0x47F85DFFL}},{{0L,0xFA8A9F6DL,1L},{0x9D5E6CF8L,0xE970D550L,0L},{0L,0L,0xB6B593C1L}},{{0xD7896F25L,0xD4342978L,0xB6B593C1L},{0x05365D73L,1L,0L},{1L,(-3L),0x47F85DFFL}},{{1L,(-1L),0xD7896F25L},{1L,(-4L),0xB6B593C1L},{1L,0xD48DE2AFL,0x05365D73L}},{{1L,0L,0x66D72F6FL},{0x05365D73L,0L,0x05365D73L},{0L,0xE970D550L,0xB6B593C1L}},{{0x1206CF87L,0xE970D550L,0xD7896F25L},{9L,0L,0x47F85DFFL},{0x56D0F252L,0L,9L}},{{9L,0xD48DE2AFL,1L},{0x1206CF87L,(-4L),1L},{0L,(-1L),9L}},{{0x05365D73L,(-3L),0x47F85DFFL},{1L,(-1L),0xD7896F25L},{1L,(-4L),0xB6B593C1L}}}; + int i, j, k; + l_792[2][1][1] &= ((((safe_sub_func_int8_t_s_s(func_87((((l_789 >= ((*l_77) = (0x3740392DL < (p_25 < (*l_782))))) | (safe_mod_func_int16_t_s_s(p_28, l_789))) , (0xEDL != 255UL))), p_25)) >= p_25) & p_26) || 0x647CL); + for (g_501 = 0; (g_501 <= 0); g_501 += 1) + { /* block id: 544 */ + int8_t *l_816 = &g_279; + l_793[1] |= (*p_27); + (*l_782) = ((safe_rshift_func_int16_t_s_s(func_41(((safe_lshift_func_int8_t_s_s(0L, 7)) , (((safe_sub_func_int16_t_s_s(((*l_77) = (safe_rshift_func_int8_t_s_s(0x05L, 6))), (p_27 != p_27))) ^ ((((!(((g_802 > (+0xAA05L)) & ((safe_mul_func_int8_t_s_s(((*l_816) = ((((safe_mul_func_int8_t_s_s((safe_rshift_func_uint16_t_u_u(g_809[0][0][0], 5)), 0x85L)) , (((safe_lshift_func_int16_t_s_s((safe_add_func_uint16_t_u_u(((safe_mod_func_uint8_t_u_u((0x08L != g_391.f0), g_241)) <= 0x1BL), 0x0014L)), p_25)) != g_408) , g_82.f0)) < (*p_27)) > p_25)), l_817[1])) , p_28)) == g_508[3][3][4].f1)) , l_818) != (void*)0) <= p_26)) || 0L)), &g_802, l_817[5], p_26, (*g_511)), 2)) && 253UL); + } + (*l_818) = (*l_818); + } + else + { /* block id: 551 */ + int32_t l_832 = 0L; + int8_t *l_838 = &g_809[0][0][7]; + int16_t l_849 = 0x28ACL; + for (g_18.f3 = 0; (g_18.f3 <= 0); g_18.f3 += 1) + { /* block id: 554 */ + int16_t l_828 = 0x7066L; + int32_t l_829 = 1L; + if ((*p_27)) + break; + (*l_782) = (safe_mul_func_uint8_t_u_u(g_240, (((safe_mul_func_int16_t_s_s((safe_mul_func_int8_t_s_s((*l_782), ((*l_782) <= p_25))), (0x2E7EL < ((g_825[1][1][0] = p_27) == p_27)))) <= g_508[3][3][4].f0) > ((*l_70) = (((l_826 , l_827) , &l_700) != &l_700))))); + ++l_833[0]; + } + l_831 &= (safe_mod_func_int8_t_s_s((func_87(p_25) , ((((p_28 != ((*l_838) ^= 0x4BL)) || p_26) <= (safe_lshift_func_uint16_t_u_u(p_28, 15))) && (safe_rshift_func_uint16_t_u_u((safe_mul_func_int8_t_s_s(((safe_rshift_func_uint16_t_u_u((safe_lshift_func_uint16_t_u_u(g_282, (*l_782))), 11)) && (((&g_628 != (void*)0) >= (*l_782)) != l_849)), 255UL)), 10)))), l_592.f6)); + if ((*p_27)) + { /* block id: 563 */ + uint32_t *l_859 = &l_793[1]; + (*g_220) = ((p_25 || 255UL) , (((safe_mod_func_uint16_t_u_u((safe_unary_minus_func_uint16_t_u((*l_782))), func_41((safe_div_func_uint32_t_u_u((safe_mod_func_uint16_t_u_u(l_817[1], p_28)), ((*l_859) |= (safe_rshift_func_uint16_t_u_u(65526UL, 1))))), &l_749[1][7][0], p_28, ((*l_78) ^= 0xA894L), (*g_511)))) , 0UL) , (*g_220))); + (*l_782) &= (l_748[4] = l_830); + } + else + { /* block id: 569 */ + uint16_t l_877 = 1UL; + uint32_t *l_878 = &g_347; + (*p_27) &= l_860; + l_830 |= ((safe_rshift_func_uint16_t_u_s(0UL, (safe_sub_func_uint16_t_u_u(l_849, ((safe_mul_func_uint16_t_u_u(0UL, ((((safe_lshift_func_uint8_t_u_u((safe_lshift_func_int8_t_s_u((safe_mod_func_int32_t_s_s(((safe_unary_minus_func_int32_t_s(0x03A43780L)) ^ ((safe_div_func_uint32_t_u_u((g_876 , l_877), l_877)) >= (p_26 = (((((*l_782) = (*p_27)) && ((*l_878) = (p_25 && g_391.f4))) , 4294967295UL) || l_817[1])))), l_877)), 7)), 1)) , g_553) || l_817[1]) , 1L))) >= p_28))))) >= 0x766AL); + } + } + for (l_743 = 2; (l_743 >= 0); l_743 -= 1) + { /* block id: 579 */ + int16_t * const *l_879 = (void*)0; + int16_t * const **l_880 = (void*)0; + int16_t * const **l_881 = &l_879; + int32_t l_884 = 1L; + int32_t l_889[3][4] = {{0xEE9CF121L,0x412F2CC4L,0x412F2CC4L,0xEE9CF121L},{1L,0x412F2CC4L,0L,0x412F2CC4L},{0x412F2CC4L,(-1L),0L,0L}}; + struct S0 * const l_900 = &g_508[3][3][4]; + int i, j; + for (l_592.f9 = 0; (l_592.f9 <= 0); l_592.f9 += 1) + { /* block id: 582 */ + if (g_391.f5) + goto lbl_673; + } + (*l_881) = l_879; + for (g_241 = 1; (g_241 <= 9); g_241 += 1) + { /* block id: 588 */ + int32_t l_882 = 0x18650A18L; + int32_t l_883 = 0x4796B007L; + int32_t l_885 = 0xB890EDFAL; + int32_t l_886 = 0xC2F30DA4L; + int32_t l_887 = 5L; + int32_t l_888 = 1L; + int32_t l_890 = (-9L); + int32_t l_891 = 0L; + int32_t l_893[4]; + int i, j; + for (i = 0; i < 4; i++) + l_893[i] = (-1L); + --l_895; + return (*g_390); + } + for (g_434 = 2; (g_434 >= 0); g_434 -= 1) + { /* block id: 594 */ + int32_t **l_898 = (void*)0; + int32_t **l_899 = &l_37; + struct S0 *l_902 = &g_508[2][2][3]; + struct S0 **l_901 = &l_902; + int i, j; + l_575[(g_434 + 1)][g_434] = l_575[(l_743 + 1)][g_434]; + (*l_899) = ((*g_220) = &l_575[(l_743 + 1)][g_434]); + (*g_220) = p_27; + (*l_901) = l_900; + } + } + } + } + } + else + { /* block id: 604 */ + struct S1 * const l_903 = &g_391; + const int8_t *l_909 = &g_910; + int32_t l_922 = 0xD6581181L; + uint32_t l_936 = 4294967291UL; + uint16_t l_937 = 65526UL; + int8_t *l_939 = &g_537[0]; + int8_t l_949 = 0xBCL; + struct S1 * const *l_993 = &g_390; + struct S0 *l_1040 = &g_82; + int32_t l_1080[8][9][1] = {{{0x02266532L},{1L},{(-7L)},{0x5385CC6DL},{(-7L)},{1L},{0x02266532L},{1L},{(-7L)}},{{0x5385CC6DL},{(-7L)},{1L},{0x02266532L},{1L},{(-7L)},{0x5385CC6DL},{(-7L)},{1L}},{{0x02266532L},{1L},{(-7L)},{0x5385CC6DL},{(-7L)},{1L},{0x02266532L},{1L},{(-7L)}},{{0x5385CC6DL},{(-7L)},{1L},{0x02266532L},{1L},{(-7L)},{0x5385CC6DL},{(-7L)},{1L}},{{0x02266532L},{0x5385CC6DL},{0x02266532L},{1L},{0x02266532L},{0x5385CC6DL},{(-1L)},{0x5385CC6DL},{0x02266532L}},{{1L},{0x02266532L},{0x5385CC6DL},{(-1L)},{0x5385CC6DL},{0x02266532L},{1L},{0x02266532L},{0x5385CC6DL}},{{(-1L)},{0x5385CC6DL},{0x02266532L},{1L},{0x02266532L},{0x5385CC6DL},{(-1L)},{0x5385CC6DL},{0x02266532L}},{{1L},{0x02266532L},{0x5385CC6DL},{(-1L)},{0x5385CC6DL},{0x02266532L},{1L},{0x02266532L},{0x5385CC6DL}}}; + int i, j, k; + (*g_220) = &l_575[0][0]; +lbl_921: + (*g_220) = (void*)0; + if ((&g_391 == (((*l_47) &= g_500) , l_903))) + { /* block id: 608 */ + uint32_t l_908[1][9][2] = {{{0UL,0UL},{0UL,0UL},{0UL,0UL},{0UL,0UL},{0UL,0UL},{0UL,0UL},{0UL,0UL},{0UL,0UL},{0UL,0UL}}}; + uint16_t *l_912 = (void*)0; + struct S0 **l_942 = (void*)0; + int32_t l_950[5] = {0xB5B7BEC4L,0xB5B7BEC4L,0xB5B7BEC4L,0xB5B7BEC4L,0xB5B7BEC4L}; + struct S1 *l_985 = &l_592; + int i, j, k; + for (g_18.f1 = (-19); (g_18.f1 < (-30)); g_18.f1 = safe_sub_func_uint32_t_u_u(g_18.f1, 4)) + { /* block id: 611 */ + struct S0 *l_919[2]; + int32_t l_920 = 0xECCA13D5L; + uint8_t *l_925[2]; + int16_t *l_953 = &g_508[3][3][4].f1; + int16_t *l_954 = (void*)0; + int16_t *l_955 = (void*)0; + int16_t *l_956 = &l_72; + struct S0 l_968 = {0xE448F1C2L,0x4F3DL,65534UL,1UL,0x56L}; + int16_t l_971 = 5L; + int32_t l_977 = 0x9EC177B0L; + int32_t l_979 = 0L; + int32_t l_981 = 0x3404FE79L; + int i; + for (i = 0; i < 2; i++) + l_919[i] = &g_18; + for (i = 0; i < 2; i++) + l_925[i] = (void*)0; + if ((safe_div_func_uint32_t_u_u((p_28 , 4294967295UL), (*p_27)))) + { /* block id: 612 */ + if (l_908[0][8][0]) + break; + } + else + { /* block id: 614 */ + struct S0 l_911 = {0xF5429923L,-10L,0UL,65535UL,0x2EL}; + int16_t *l_913[4][1][10] = {{{&l_72,&l_72,&l_911.f1,(void*)0,&l_911.f1,&l_72,&l_72,&l_911.f1,(void*)0,&l_911.f1}},{{&l_72,&l_72,&l_911.f1,(void*)0,&l_911.f1,&l_72,&l_72,&l_911.f1,(void*)0,&l_911.f1}},{{&l_72,&l_72,&l_911.f1,(void*)0,&l_911.f1,&l_72,&l_72,&l_911.f1,(void*)0,&l_911.f1}},{{&l_72,&l_72,&l_911.f1,(void*)0,&l_911.f1,&l_72,&l_72,(void*)0,&l_72,(void*)0}}}; + int32_t l_914 = (-5L); + int32_t l_915 = 1L; + struct S1 **l_918 = &g_390; + int i, j, k; + (*l_782) &= (l_908[0][8][1] != (((l_915 = (l_914 = l_908[0][8][0])) != ((((safe_add_func_uint32_t_u_u((0x7C33L || (l_918 != (((((void*)0 != l_919[0]) , g_555[0][1]) , p_25) , &g_390))), l_911.f3)) || p_28) & 0xACDCL) < 0xC0L)) < l_920)); + if (g_391.f2) + goto lbl_921; + } + } + } + else + { /* block id: 664 */ + uint32_t l_1000 = 0x0E49DC54L; + int32_t l_1051 = 8L; + int32_t l_1053 = 0x7C5178F1L; + int8_t **l_1061 = &g_512; + for (g_876 = 17; (g_876 < 20); ++g_876) + { /* block id: 667 */ + for (l_922 = 0; (l_922 != 2); l_922 = safe_add_func_uint32_t_u_u(l_922, 9)) + { /* block id: 670 */ + struct S1 * const *l_992 = &l_903; + struct S1 * const **l_991[10] = {&l_992,&l_992,&l_992,&l_992,&l_992,&l_992,&l_992,&l_992,&l_992,&l_992}; + int32_t l_994 = 0xFEEF50B1L; + int i; + l_993 = &l_903; + for (g_222 = 0; (g_222 <= 0); g_222 += 1) + { /* block id: 674 */ + int32_t *l_995 = (void*)0; + int32_t *l_996 = &g_238; + int32_t *l_997 = &g_434; + int32_t *l_998 = &l_994; + int32_t *l_999 = &g_433; + struct S1 **l_1003 = &g_390; + (*g_220) = (*g_220); + l_1000--; + (*l_1003) = &g_391; + } + } + } + if ((l_1000 , ((p_25 , l_949) || ((*l_782) = (safe_mod_func_uint32_t_u_u((safe_mod_func_uint8_t_u_u(func_41(p_25, &l_937, l_922, (safe_sub_func_uint16_t_u_u((safe_rshift_func_int8_t_s_u(g_5, 7)), (func_41(func_41((**g_365), &l_937, p_25, l_922, l_1012), &l_937, l_1000, p_25, (*g_511)) , (*l_782)))), (*g_511)), p_25)), p_25)))))) + { /* block id: 682 */ + int32_t l_1013 = 0x9ACA0752L; + int32_t l_1014 = (-5L); + l_1014 ^= (l_1013 <= p_28); + return (**l_993); + } + else + { /* block id: 685 */ + (*p_27) = (*p_27); + } + for (g_279 = 0; (g_279 > 29); g_279 = safe_add_func_uint32_t_u_u(g_279, 8)) + { /* block id: 690 */ + uint16_t l_1029 = 0x98C6L; + int16_t **l_1035 = &g_313[1][2][2]; + int16_t ***l_1034[9][1][1] = {{{&l_1035}},{{(void*)0}},{{&l_1035}},{{(void*)0}},{{&l_1035}},{{(void*)0}},{{&l_1035}},{{(void*)0}},{{&l_1035}}}; + int32_t l_1036 = 0L; + uint32_t l_1054[1]; + int16_t l_1072[8] = {1L,1L,1L,1L,1L,1L,1L,1L}; + int i, j, k; + for (i = 0; i < 1; i++) + l_1054[i] = 1UL; + for (g_209 = 16; (g_209 >= 50); ++g_209) + { /* block id: 693 */ + int8_t *l_1027[6][6][5] = {{{(void*)0,(void*)0,&g_280,(void*)0,(void*)0},{&g_809[0][0][0],&g_36[5],&g_279,&g_36[0],&g_280},{(void*)0,(void*)0,&g_537[0],(void*)0,(void*)0},{&g_280,&g_36[0],&g_279,&g_36[5],&g_809[0][0][0]},{(void*)0,(void*)0,&g_280,(void*)0,(void*)0},{&g_809[0][0][0],&g_36[0],(void*)0,&g_36[0],&g_809[0][0][0]}},{{(void*)0,(void*)0,&g_280,(void*)0,(void*)0},{&g_809[0][0][0],&g_36[5],&g_279,&g_36[0],&g_280},{(void*)0,(void*)0,&g_537[0],(void*)0,(void*)0},{&g_280,&g_36[0],&g_279,&g_36[5],&g_809[0][0][0]},{(void*)0,(void*)0,&g_280,(void*)0,(void*)0},{&g_809[0][0][0],&g_36[0],(void*)0,&g_36[0],&g_809[0][0][0]}},{{(void*)0,(void*)0,&g_280,(void*)0,(void*)0},{&l_949,(void*)0,&g_280,&g_809[2][0][2],&g_809[0][0][0]},{&g_40,(void*)0,(void*)0,(void*)0,&g_40},{&g_809[0][0][0],&g_809[2][0][2],&g_280,(void*)0,&l_949},{&g_40,&l_949,&g_809[4][0][4],(void*)0,&g_36[3]},{&l_949,&g_809[2][0][2],&g_279,&g_809[2][0][2],&l_949}},{{&g_36[3],(void*)0,&g_809[4][0][4],&l_949,&g_40},{&l_949,(void*)0,&g_280,&g_809[2][0][2],&g_809[0][0][0]},{&g_40,(void*)0,(void*)0,(void*)0,&g_40},{&g_809[0][0][0],&g_809[2][0][2],&g_280,(void*)0,&l_949},{&g_40,&l_949,&g_809[4][0][4],(void*)0,&g_36[3]},{&l_949,&g_809[2][0][2],&g_279,&g_809[2][0][2],&l_949}},{{&g_36[3],(void*)0,&g_809[4][0][4],&l_949,&g_40},{&l_949,(void*)0,&g_280,&g_809[2][0][2],&g_809[0][0][0]},{&g_40,(void*)0,(void*)0,(void*)0,&g_40},{&g_809[0][0][0],&g_809[2][0][2],&g_280,(void*)0,&l_949},{&g_40,&l_949,&g_809[4][0][4],(void*)0,&g_36[3]},{&l_949,&g_809[2][0][2],&g_279,&g_809[2][0][2],&l_949}},{{&g_36[3],(void*)0,&g_809[4][0][4],&l_949,&g_40},{&l_949,(void*)0,&g_280,&g_809[2][0][2],&g_809[0][0][0]},{&g_40,&g_40,&g_36[3],&g_40,(void*)0},{&g_40,&g_279,&g_809[0][0][0],&g_40,&g_279},{(void*)0,&g_809[4][0][4],&g_36[3],&g_40,&g_537[1]},{&g_279,&g_279,&g_36[3],&g_279,&g_279}}}; + int32_t l_1028[6]; + struct S0 **l_1041 = &l_1040; + int i, j, k; + for (i = 0; i < 6; i++) + l_1028[i] = 0x27D0BFBFL; + } + for (g_540 = 0; (g_540 <= (-29)); g_540 = safe_sub_func_uint8_t_u_u(g_540, 1)) + { /* block id: 716 */ + int8_t **l_1062 = &l_1012; + int8_t ***l_1063 = (void*)0; + int8_t ***l_1064 = &l_1062; + int32_t l_1067 = 7L; + uint8_t *l_1071 = &g_18.f4; + int32_t *l_1073 = &l_1067; + int32_t *l_1074 = &l_1051; + int32_t *l_1075 = &g_39; + int32_t *l_1076 = &l_1067; + int32_t *l_1077 = &g_238; + int32_t *l_1078 = &g_876; + int32_t *l_1079[2][3][4] = {{{&g_500,&g_876,&g_433,&g_876},{&g_876,&g_876,&g_433,&g_433},{&g_500,&g_500,&g_876,&g_433}},{{&g_876,&g_876,&g_876,&g_876},{&g_876,&g_876,&g_876,&g_876},{&g_500,&g_876,&g_433,&g_876}}}; + int i, j, k; + (*g_220) = p_27; + l_1072[2] ^= (((*l_939) |= l_1029) , (((*l_1071) = (((*l_782) &= (g_18 , (*p_27))) & (l_922 |= ((1L < ((l_1054[0] && (l_1061 != ((*l_1064) = l_1062))) , ((g_802 = (safe_mod_func_uint16_t_u_u(((l_1067 | (safe_sub_func_uint8_t_u_u((l_937 && p_25), 0xAEL))) >= 0x51EFA7E7L), 0xE701L))) <= l_1070[0]))) <= (**g_220))))) , l_1067)); + ++l_1081; + } + if (l_1072[7]) + continue; + return (*g_390); + } + } + } +lbl_1085: + (*l_1084) &= (*l_782); + (*g_220) = p_27; + return (*g_390); +} + + +/* ------------------------------------------ */ +/* + * reads : g_82.f1 + * writes: g_82.f1 g_500 + */ +static uint32_t func_41(int32_t p_42, uint16_t * p_43, int32_t p_44, uint16_t p_45, int8_t * p_46) +{ /* block id: 359 */ + int32_t l_580 = 4L; + for (g_82.f1 = (-1); (g_82.f1 < 7); g_82.f1++) + { /* block id: 362 */ + int8_t l_578 = 0x96L; + int32_t *l_579 = &g_500; + (*l_579) = l_578; + l_580 &= (~p_42); + } + return l_580; +} + + +/* ------------------------------------------ */ +/* + * reads : g_18.f1 g_82 g_36 g_5 g_18.f4 g_71 g_18.f0 g_39 g_119 g_79 g_18.f3 g_2 g_195 g_209 g_18.f2 g_222 g_40 g_220 g_199 g_208 g_241 g_240 g_254 g_282 g_237 g_280 g_347 g_238 g_365 g_390 g_408 g_391.f4 g_391.f9 g_391 g_501 g_508.f0 + * writes: g_36 g_39 g_82.f1 g_119 g_82.f4 g_2 g_79 g_195 g_199 g_209 g_18.f3 g_5 g_82.f3 g_220 g_222 g_241 g_251 g_40 g_282 g_280 g_313 g_237 g_347 g_71 g_238 g_365 g_18.f4 g_390 g_366 g_408 g_208 g_501 + */ +static const uint16_t func_52(const int8_t * p_53, struct S0 p_54, uint16_t * p_55) +{ /* block id: 23 */ + int8_t *l_91 = &g_36[3]; + int8_t *l_92[5]; + int32_t l_574 = 0L; + int i; + for (i = 0; i < 5; i++) + l_92[i] = (void*)0; + l_574 = ((((*p_55) || (((p_54.f4 = g_18.f1) , (safe_lshift_func_int16_t_s_u(((p_54.f1 & (safe_mod_func_uint32_t_u_u(func_87((safe_mul_func_int16_t_s_s((((*p_55) > (g_82 , (*p_55))) >= (((*l_91) = (*p_53)) ^ p_54.f3)), ((l_92[1] = l_91) == (void*)0)))), l_574))) | 0xF0L), g_82.f2))) > p_54.f3)) ^ (*p_55)) != 0x7A4DDB26L); + return (*p_55); +} + + +/* ------------------------------------------ */ +/* + * reads : g_18.f4 g_36 g_39 g_209 g_408 g_18.f0 g_195 g_241 + * writes: g_18.f4 g_39 g_209 g_408 g_18.f0 g_195 g_241 + */ +static const int8_t * func_56(int32_t * p_57, int16_t p_58, uint16_t * p_59, int8_t * p_60) +{ /* block id: 16 */ + for (g_18.f4 = 27; (g_18.f4 >= 55); g_18.f4++) + { /* block id: 19 */ + (*p_57) ^= g_36[3]; + } + return p_60; +} + + +/* ------------------------------------------ */ +/* + * reads : g_5 g_18.f4 g_71 g_18.f0 g_39 g_82.f0 g_82.f1 g_36 g_119 g_82.f3 g_82.f4 g_79 g_18.f3 g_2 g_195 g_209 g_18.f2 g_222 g_40 g_220 g_199 g_208 g_241 g_240 g_254 g_282 g_82.f2 g_237 g_280 g_347 g_238 g_365 g_390 g_408 g_391.f4 g_391.f9 g_18.f1 g_391 g_501 g_508.f0 + * writes: g_39 g_82.f1 g_119 g_36 g_82.f4 g_2 g_79 g_195 g_199 g_209 g_18.f3 g_5 g_82.f3 g_220 g_222 g_241 g_251 g_40 g_282 g_280 g_313 g_237 g_347 g_71 g_238 g_365 g_18.f4 g_390 g_366 g_408 g_208 g_501 + */ +static uint32_t func_87(uint32_t p_88) +{ /* block id: 27 */ + int16_t l_99 = (-3L); + uint16_t *l_100 = &g_79; + uint16_t *l_101 = &g_79; + int16_t l_102 = 0x2A7AL; + uint32_t l_109 = 0x8BEEE5F8L; + struct S0 l_124[7][3] = {{{0xAE64750AL,-1L,65528UL,0UL,4UL},{0xAE64750AL,-1L,65528UL,0UL,4UL},{0xAE64750AL,-1L,65528UL,0UL,4UL}},{{0xAE64750AL,-1L,65528UL,0UL,4UL},{0xAE64750AL,-1L,65528UL,0UL,4UL},{0xAE64750AL,-1L,65528UL,0UL,4UL}},{{0xAE64750AL,-1L,65528UL,0UL,4UL},{0xAE64750AL,-1L,65528UL,0UL,4UL},{0xAE64750AL,-1L,65528UL,0UL,4UL}},{{0xAE64750AL,-1L,65528UL,0UL,4UL},{0xAE64750AL,-1L,65528UL,0UL,4UL},{0xAE64750AL,-1L,65528UL,0UL,4UL}},{{0xAE64750AL,-1L,65528UL,0UL,4UL},{0xAE64750AL,-1L,65528UL,0UL,4UL},{0xAE64750AL,-1L,65528UL,0UL,4UL}},{{0xAE64750AL,-1L,65528UL,0UL,4UL},{0xAE64750AL,-1L,65528UL,0UL,4UL},{0xAE64750AL,-1L,65528UL,0UL,4UL}},{{0xAE64750AL,-1L,65528UL,0UL,4UL},{0xAE64750AL,-1L,65528UL,0UL,4UL},{0xAE64750AL,-1L,65528UL,0UL,4UL}}}; + int32_t l_163 = 0x4B2EDB8FL; + int8_t l_177[7]; + uint32_t l_229 = 18446744073709551615UL; + int32_t l_233 = (-1L); + int32_t l_235 = 0xD2773083L; + int32_t l_236 = 0x58A2F2A6L; + int32_t l_246 = (-1L); + uint8_t *l_269 = (void*)0; + uint8_t **l_268 = &l_269; + int8_t *l_298 = &g_36[6]; + int8_t **l_297[9] = {&l_298,&l_298,&l_298,&l_298,&l_298,&l_298,&l_298,&l_298,&l_298}; + struct S1 l_311 = {3,-2389,162,110,1321,-10,-1534,54,17,1UL}; + uint32_t l_386 = 18446744073709551611UL; + int8_t l_407 = (-9L); + uint32_t l_435 = 0x04C585D8L; + uint32_t l_454 = 0x06D19C9CL; + int32_t l_482 = 8L; + int16_t l_493 = 0x7372L; + struct S0 *l_507[1]; + int i, j; + for (i = 0; i < 7; i++) + l_177[i] = 0x47L; + for (i = 0; i < 1; i++) + l_507[i] = &g_508[3][3][4]; + if ((!(((safe_add_func_uint32_t_u_u(g_5, (4L && (safe_div_func_int8_t_s_s(g_18.f4, g_71))))) == ((safe_rshift_func_int16_t_s_s(p_88, 3)) , (((((((p_88 , ((l_99 , (void*)0) != (l_101 = l_100))) & 0x42C135D3L) ^ l_99) | p_88) <= l_99) , (void*)0) == &g_5))) > l_102))) + { /* block id: 29 */ + const int32_t l_104 = 1L; + int32_t l_111 = 0L; + uint8_t *l_154 = &g_5; + uint32_t l_184 = 1UL; + int32_t *l_203 = &g_39; + int32_t *l_204 = (void*)0; + int32_t *l_205 = &l_111; + int32_t *l_206 = (void*)0; + int32_t *l_207[6][5][8] = {{{&g_39,&l_163,&g_2,(void*)0,&l_111,&g_2,&g_2,&l_163},{&l_111,&l_111,&g_39,&g_39,&g_39,&g_2,&g_2,&l_163},{&l_163,&g_39,&g_2,(void*)0,&g_2,&g_39,&l_163,&l_163},{&l_111,&l_163,&g_2,&l_163,&g_2,&g_2,&g_39,&g_39},{(void*)0,&g_2,&g_39,&g_2,&g_39,&l_111,&l_111,(void*)0}},{{&l_163,(void*)0,&l_163,&g_39,(void*)0,&l_111,&l_163,&l_163},{(void*)0,&l_163,&g_2,(void*)0,&g_2,&g_2,&g_2,&g_39},{&g_2,&g_39,&g_39,&g_39,&l_163,&g_2,&g_2,&l_111},{&g_2,&g_2,(void*)0,&g_39,(void*)0,(void*)0,&g_2,&g_39},{&g_2,&g_2,&g_39,&g_39,&g_2,&g_2,&l_163,&g_39}},{{&l_163,&g_2,&l_111,&g_2,&g_39,&g_2,&g_39,(void*)0},{&g_2,&g_39,&l_163,(void*)0,(void*)0,&l_163,&g_39,&g_2},{&g_2,(void*)0,&g_2,&l_111,&g_39,&l_111,&l_163,(void*)0},{&l_163,&g_39,&g_39,&g_2,&l_111,&l_111,(void*)0,(void*)0},{&g_39,(void*)0,&g_2,&g_2,&g_39,&g_2,(void*)0,&l_111}},{{&l_111,&l_111,&l_163,&g_39,&g_39,(void*)0,&g_2,&l_111},{(void*)0,&g_2,&l_163,&g_2,&g_39,&g_39,&l_111,(void*)0},{&g_39,&l_163,&l_111,&g_39,&g_39,&g_39,&l_163,&l_163},{&l_111,(void*)0,&g_2,&l_111,(void*)0,&l_163,&g_39,&g_2},{&g_39,(void*)0,(void*)0,&g_2,&l_111,&g_2,&g_39,&g_2}},{{&g_39,&l_111,&g_39,&g_39,&g_2,&l_111,&l_111,&g_2},{&l_163,&g_2,&g_2,(void*)0,(void*)0,&g_39,&g_2,&g_2},{&l_163,&g_39,&l_111,&g_39,&g_2,(void*)0,&g_39,&g_2},{&g_2,&g_2,(void*)0,&l_111,&g_39,&l_163,&g_39,&l_111},{&l_163,&l_111,&g_2,&g_39,&l_111,&g_39,&l_111,&g_39}},{{&g_39,&g_2,&g_39,&g_2,&g_39,&g_2,&l_163,&g_39},{(void*)0,&g_39,&g_39,&g_2,&g_2,&g_2,&g_2,&g_2},{&l_111,&g_2,(void*)0,&g_39,&g_2,(void*)0,&l_111,(void*)0},{&g_2,&l_163,&l_163,(void*)0,&g_39,(void*)0,&g_2,&g_2},{&g_39,&l_163,&g_39,&l_111,&l_163,&g_2,&l_163,&g_2}}}; + int i, j, k; + if ((0UL >= g_18.f0)) + { /* block id: 30 */ + int32_t *l_103 = &g_39; + int32_t **l_108 = &l_103; + (*l_103) ^= p_88; + if (l_104) + { /* block id: 32 */ + (*l_103) |= g_82.f0; + } + else + { /* block id: 34 */ + int8_t * const l_107 = &g_40; + int8_t * const *l_106 = &l_107; + int8_t * const **l_105 = &l_106; + (*l_105) = (void*)0; + } + (*l_108) = &g_39; + } + else + { /* block id: 38 */ + uint32_t l_112 = 0UL; + int32_t l_118[9] = {0x14109486L,0x33D78BCCL,0x14109486L,0x14109486L,0x33D78BCCL,0x14109486L,0x14109486L,0x33D78BCCL,0x14109486L}; + uint8_t *l_155 = &l_124[4][2].f4; + int i; + if (((l_109 || l_104) == l_102)) + { /* block id: 39 */ + int32_t *l_110[3]; + int i; + for (i = 0; i < 3; i++) + l_110[i] = &g_2; + l_112 ^= (l_111 |= 1L); + } + else + { /* block id: 42 */ + uint32_t l_113 = 1UL; + int32_t l_117 = 0xE729AE47L; + int32_t l_192 = (-2L); + int32_t l_193 = 0x9EB32EB6L; + int32_t l_194 = (-3L); + for (g_82.f1 = 0; (g_82.f1 <= 7); g_82.f1 += 1) + { /* block id: 45 */ + int32_t *l_164 = &l_111; + int i; + if ((g_36[g_82.f1] , 9L)) + { /* block id: 46 */ + int32_t *l_116[6] = {(void*)0,&g_2,(void*)0,(void*)0,&g_2,(void*)0}; + int i; + ++l_113; + l_117 = g_36[g_82.f1]; + --g_119; + } + else + { /* block id: 50 */ + int32_t *l_122 = &l_111; + int32_t *l_123 = &l_117; + int8_t *l_129[5]; + uint8_t *l_130 = &g_82.f4; + int i; + for (i = 0; i < 5; i++) + l_129[i] = &g_36[g_82.f1]; + (*l_123) = ((*l_122) ^= g_36[g_82.f1]); + (*l_123) ^= ((l_124[4][2] , ((g_36[g_82.f1] , (p_88 && (safe_sub_func_int8_t_s_s((safe_add_func_int8_t_s_s(g_82.f3, ((((g_36[3] = g_18.f4) != ((*l_130)++)) && (safe_lshift_func_uint16_t_u_s((safe_mul_func_uint16_t_u_u((safe_rshift_func_int8_t_s_u((safe_lshift_func_int8_t_s_s((~((safe_rshift_func_uint16_t_u_u((safe_mod_func_uint8_t_u_u(g_5, (((*l_122) ^= (((safe_sub_func_int16_t_s_s(0xF46BL, ((((((g_18.f0 >= (safe_mod_func_uint16_t_u_u(p_88, l_124[4][2].f1))) || 65535UL) >= l_113) < p_88) < 0xB794862DL) && g_82.f0))) , 0UL) && 0UL)) , g_36[3]))), 13)) ^ p_88)), l_113)), 3)), p_88)), g_18.f0))) & 3L))), p_88)))) && g_36[g_82.f1])) | p_88); + if ((*l_123)) + break; + } + if (g_39) + goto lbl_198; + for (g_119 = 0; (g_119 == 12); g_119++) + { /* block id: 61 */ + int32_t *l_151 = &g_2; + uint8_t **l_156 = &l_155; + (*l_151) = (l_117 = 0x6B1119ABL); + (*l_151) = (p_88 == ((1UL > (l_154 != ((*l_156) = l_155))) | (((safe_mod_func_uint16_t_u_u(g_36[g_82.f1], (((*l_101) |= (safe_mod_func_uint16_t_u_u(p_88, 0x0427L))) || 0xE7B8L))) || (safe_sub_func_uint32_t_u_u(p_88, 0x82777045L))) != l_112))); + } + (*l_164) = (l_163 |= ((l_113 > 1L) >= ((-1L) | ((g_36[3] != 0x27924F71L) , l_104)))); + for (p_88 = 0; (p_88 <= 7); p_88 += 1) + { /* block id: 72 */ + uint32_t *l_178 = &l_112; + int32_t *l_179 = (void*)0; + int32_t l_180 = 0x58832062L; + int32_t *l_181 = &l_118[p_88]; + int32_t *l_182 = (void*)0; + int32_t *l_183 = &l_118[6]; + int i; + if (g_36[g_82.f1]) + break; + l_180 = (safe_add_func_uint32_t_u_u(g_18.f3, ((*l_164) = (safe_rshift_func_int8_t_s_s((((safe_rshift_func_uint16_t_u_s(((*l_101)--), 5)) < 1L) == (l_117 | ((*l_178) = (((g_18.f0 || (l_118[p_88] = p_88)) ^ l_124[4][2].f1) < (safe_sub_func_int8_t_s_s(((((l_112 == ((l_163 = (((void*)0 == &g_71) == l_177[0])) >= l_104)) > l_113) & 252UL) & 0xC0L), 0x89L)))))), g_2))))); + l_184--; + (*l_183) = (*l_164); + } + } +lbl_198: + for (l_112 = 0; (l_112 < 24); l_112++) + { /* block id: 86 */ + int32_t *l_189 = (void*)0; + int32_t *l_190 = &l_111; + int32_t *l_191[9][7] = {{(void*)0,&l_118[8],&l_163,&l_163,&l_118[8],(void*)0,&l_118[0]},{(void*)0,&l_118[0],&l_163,&l_118[0],&g_39,&l_118[0],&l_118[8]},{&l_118[0],&l_117,(void*)0,(void*)0,&g_2,&l_118[8],&g_2},{&g_39,&l_118[0],&l_118[0],&g_39,&l_163,&l_118[0],(void*)0},{&g_39,&l_118[8],&l_118[0],&l_117,&g_39,&l_117,&l_163},{&l_118[0],&l_163,(void*)0,&l_118[0],(void*)0,&l_118[0],(void*)0},{(void*)0,(void*)0,&l_117,&l_117,&l_117,&l_118[0],&g_2},{(void*)0,&l_117,&g_2,&l_118[0],&l_163,&g_39,&l_118[0]},{&l_117,&l_163,&g_39,(void*)0,&g_39,&l_163,&l_117}}; + int i, j; + ++g_195; + } + if (g_18.f4) + { /* block id: 90 */ + g_199 = &g_2; + } + else + { /* block id: 92 */ + const int32_t *l_201 = (void*)0; + const int32_t **l_200 = &l_201; + int32_t *l_202 = &l_192; + (*l_200) = (void*)0; + (*l_202) ^= (-2L); + } + l_193 = (((p_88 , ((void*)0 == &l_192)) ^ ((*l_101) = 0x9C0BL)) ^ 0x22F9L); + } + } + g_209++; + for (g_82.f4 = 2; (g_82.f4 <= 7); g_82.f4 += 1) + { /* block id: 103 */ + uint16_t l_216 = 1UL; + int32_t l_232 = 5L; + int32_t l_239 = (-1L); + int8_t ***l_270 = (void*)0; + for (g_18.f3 = 0; (g_18.f3 <= 4); g_18.f3 += 1) + { /* block id: 106 */ + uint32_t *l_217 = &l_184; + int32_t l_221 = (-9L); + int32_t l_234[5][5][8] = {{{0xB9C36536L,1L,0x3C2C6411L,0L,(-2L),0x63EE46ABL,5L,0xA1254473L},{0L,0x337FFAD2L,1L,0L,0x83C53249L,0xA1254473L,(-1L),0xD57E7891L},{0x540E818FL,0x559AB983L,0xA1254473L,0x5C0450BFL,0xA1254473L,0x559AB983L,0x540E818FL,(-1L)},{0xCA09B53EL,(-1L),1L,5L,(-7L),1L,0xA3AA631CL,0x99F47FE8L},{0x559AB983L,0xB1993FB8L,0x23F3548BL,0xA3AA631CL,(-7L),0xD57E7891L,0xF18445C7L,0x5C0450BFL}},{{0xCA09B53EL,0x5A902683L,0L,0x99F47FE8L,0xA1254473L,0x23F3548BL,(-1L),0x83C53249L},{0x540E818FL,0xCA09B53EL,0xB9C36536L,0x5A902683L,0x83C53249L,0x5C0450BFL,0xEE774041L,0L},{0L,1L,0xCA09B53EL,(-2L),(-2L),0xCA09B53EL,1L,0L},{0xB9C36536L,(-5L),1L,0xCA09B53EL,1L,0L,0x3C2C6411L,(-1L)},{0x3C2C6411L,0L,0x83C53249L,2L,0xCA09B53EL,0L,0x337FFAD2L,0x5A902683L}},{{0L,(-5L),0L,1L,5L,0L,0xCA09B53EL,0x3C2C6411L},{1L,0L,1L,1L,2L,0L,0L,0L},{0xB1993FB8L,0L,1L,0x5C0450BFL,1L,0x5C0450BFL,1L,0L},{(-5L),1L,1L,0L,0xA1254473L,5L,0x63EE46ABL,(-2L)},{(-7L),0x23F3548BL,(-1L),3L,(-5L),0xA3AA631CL,0x63EE46ABL,0xA1254473L}},{{0L,3L,1L,5L,0x337FFAD2L,0x99F47FE8L,1L,0L},{0x337FFAD2L,0x99F47FE8L,1L,0L,1L,0x5A902683L,0L,0x559AB983L},{0L,0L,1L,0xCA09B53EL,(-2L),(-2L),0xCA09B53EL,1L},{(-1L),(-1L),0xF18445C7L,1L,0x23F3548BL,0xCA09B53EL,0L,0x99F47FE8L},{0xA1254473L,0x540E818FL,(-7L),1L,0x559AB983L,2L,0xD57E7891L,0x99F47FE8L}},{{0x540E818FL,0x3C2C6411L,0xA3AA631CL,1L,1L,1L,0L,1L},{1L,0x89557B5DL,0L,0xCA09B53EL,0x3C2C6411L,(-1L),0x540E818FL,0x559AB983L},{0L,1L,0xEE774041L,0L,0xF18445C7L,0x99F47FE8L,0xF18445C7L,0x63EE46ABL},{0xEE774041L,0xCA09B53EL,0xEE774041L,1L,0x559AB983L,3L,2L,1L},{0x5A902683L,0x63EE46ABL,1L,0x5C0450BFL,0L,0x23F3548BL,0x559AB983L,0x89557B5DL}}}; + const uint8_t *l_267 = &g_5; + int i, j, k; + if (((((((safe_add_func_int8_t_s_s(g_36[g_82.f4], (p_88 == (safe_mul_func_int8_t_s_s((((l_216 ^ ((void*)0 != &l_111)) == (((((*l_217) = (((*l_154) &= p_88) > g_18.f2)) >= ((-1L) > (safe_lshift_func_uint16_t_u_s((l_102 | l_99), l_124[4][2].f1)))) <= g_36[g_82.f4]) >= g_36[g_82.f4])) & 0x73132987L), p_88))))) || 0xDFED307DL) == g_39) || 0xB2D16ADBL) , 0x85EAL) <= 0x6F1BL)) + { /* block id: 109 */ + for (g_82.f3 = 0; (g_82.f3 <= 7); g_82.f3 += 1) + { /* block id: 112 */ + int i; + return l_177[(g_18.f3 + 1)]; + } + g_220 = &g_199; + (*l_203) = p_88; + if (((*l_205) &= l_177[4])) + { /* block id: 118 */ + g_222++; + if (p_88) + break; + (*l_203) &= (g_2 = ((p_88 <= (safe_lshift_func_int16_t_s_s((l_221 != (g_82.f1 && g_40)), ((g_79++) >= g_18.f4)))) , p_88)); + } + else + { /* block id: 124 */ + (*g_220) = (void*)0; + } + } + else + { /* block id: 127 */ + (*g_220) = (*g_220); + for (g_195 = 0; (g_195 <= 7); g_195 += 1) + { /* block id: 131 */ + int i, j, k; + l_207[(g_18.f3 + 1)][g_18.f3][g_82.f4] = (void*)0; + l_229 ^= (l_207[g_18.f3][g_18.f3][(g_18.f3 + 1)] == l_207[g_18.f3][g_18.f3][g_195]); + l_163 = (safe_sub_func_int8_t_s_s(g_208, g_5)); + } + l_232 ^= ((*l_205) = p_88); + } + g_241++; + if ((safe_mod_func_uint32_t_u_u((g_251 = ((p_88 , l_246) || ((safe_lshift_func_uint16_t_u_u((safe_rshift_func_int8_t_s_u(g_82.f4, (g_18.f4 > g_240))), l_216)) > (p_88 ^ (*l_203))))), l_234[4][4][1]))) + { /* block id: 141 */ + for (g_40 = 5; (g_40 >= 1); g_40 -= 1) + { /* block id: 144 */ + return p_88; + } + for (l_239 = 4; (l_239 >= 0); l_239 -= 1) + { /* block id: 149 */ + uint8_t * const *l_255 = (void*)0; + int32_t *l_256[7] = {(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0}; + int i, j, k; + (*l_203) ^= (l_207[(g_18.f3 + 1)][l_239][(g_18.f3 + 2)] == &g_251); + (*l_205) = (g_36[g_82.f4] , (safe_mod_func_uint32_t_u_u(((*l_217) = (g_254 == l_255)), g_36[l_239]))); + (*l_205) = (l_99 != ((l_235 = 0x5B3381A5L) , (safe_mod_func_int16_t_s_s(0x7712L, (safe_div_func_uint16_t_u_u((safe_div_func_uint16_t_u_u((((*l_203) = (((safe_rshift_func_int16_t_s_s(p_88, (safe_mul_func_uint8_t_u_u(((l_154 != l_267) >= (g_254 == l_268)), (!0xA9L))))) , (void*)0) != l_270)) >= g_222), l_124[4][2].f2)), p_88)))))); + } + (*l_203) ^= (g_71 <= 0L); + } + else + { /* block id: 158 */ + (*l_205) = ((((l_221 == p_88) <= ((safe_mod_func_uint8_t_u_u(5UL, 1UL)) && (safe_add_func_uint16_t_u_u(((&l_239 == &l_111) ^ (safe_mod_func_uint8_t_u_u((g_79 == (safe_sub_func_uint32_t_u_u((((*l_203) > 0x1999272AL) & p_88), 0L))), 0xFEL))), p_88)))) || (*l_205)) && 2L); + } + g_282--; + } + (*g_220) = (*g_220); + if ((*l_203)) + break; + } + } + else + { /* block id: 166 */ + uint8_t l_285 = 0x4CL; + int32_t l_299 = 0x89F5BDD3L; + int32_t l_306 = 0x5BC425B6L; + int32_t l_340 = (-4L); + int32_t l_341 = (-1L); + int32_t l_344 = 0xE829D8AAL; + int32_t l_346[7][8] = {{(-1L),(-1L),0xF04E8ED3L,0L,(-1L),0x3E5B3455L,(-1L),0L},{(-8L),(-1L),(-8L),0x3E5B3455L,(-1L),0x7B4FA158L,(-1L),0x7B4FA158L},{0x2880767BL,0x3E5B3455L,0xF04E8ED3L,0x3E5B3455L,0x2880767BL,0xF936D604L,(-1L),0L},{0x2880767BL,0xF936D604L,(-1L),0L,(-1L),0xF936D604L,0x2880767BL,0x3E5B3455L},{(-8L),0x3E5B3455L,(-1L),0x7B4FA158L,(-1L),0x7B4FA158L,(-1L),0x3E5B3455L},{(-1L),(-1L),0xF04E8ED3L,0L,(-1L),0x3E5B3455L,(-1L),0L},{(-8L),(-1L),(-8L),0x3E5B3455L,(-1L),0x7B4FA158L,(-1L),0x7B4FA158L}}; + int32_t l_377 = 0x828E5972L; + struct S1 *l_404 = &g_391; + int32_t l_427 = 0x3D04868CL; + int16_t l_439 = 0L; + const uint8_t l_449 = 0x21L; + struct S0 *l_506 = &g_82; + struct S0 **l_505[2]; + int32_t l_554 = 1L; + int i, j; + for (i = 0; i < 2; i++) + l_505[i] = &l_506; +lbl_504: + if (l_285) + { /* block id: 167 */ + for (l_99 = (-24); (l_99 != 2); l_99 = safe_add_func_int8_t_s_s(l_99, 6)) + { /* block id: 170 */ + uint32_t l_288 = 0xFD1AB546L; + return l_288; + } + } + else + { /* block id: 173 */ + int32_t l_320[4][1][9] = {{{0L,0x194BB3C6L,0x8BCB9031L,0xB986918BL,0xB986918BL,0x8BCB9031L,0x194BB3C6L,0L,7L}},{{(-10L),0xB986918BL,3L,0L,0x67FAAF16L,7L,7L,0x67FAAF16L,0xB986918BL}},{{0L,0x039CAC54L,0L,0x8BCB9031L,7L,7L,(-10L),0x83995F0FL,0x83995F0FL}},{{0L,0x039CAC54L,0x83995F0FL,7L,0x83995F0FL,0x039CAC54L,0L,(-10L),0x194BB3C6L}}}; + int32_t l_329 = (-1L); + int32_t l_342 = (-10L); + int32_t l_343 = 1L; + int32_t l_345 = (-1L); + int i, j, k; + for (g_5 = 0; (g_5 <= 20); ++g_5) + { /* block id: 176 */ + (*g_220) = (*g_220); + l_299 ^= (0x6735L > (((g_82.f2 & (l_285 == (safe_mul_func_int16_t_s_s((-6L), (safe_mul_func_int8_t_s_s((((safe_mod_func_uint32_t_u_u((((void*)0 == l_297[4]) <= g_237), p_88)) , 3UL) ^ g_119), l_285)))))) == 4294967295UL) >= p_88)); + } + if ((safe_lshift_func_int8_t_s_u(g_82.f2, (safe_lshift_func_uint8_t_u_u(l_233, l_177[0]))))) + { /* block id: 180 */ + uint16_t l_323 = 1UL; + for (g_280 = 23; (g_280 <= (-26)); g_280 = safe_sub_func_uint8_t_u_u(g_280, 3)) + { /* block id: 183 */ + uint32_t l_328 = 0x04263EA8L; + if (l_306) + { /* block id: 184 */ + uint8_t *l_310 = &g_119; + int16_t *l_312 = (void*)0; + int32_t *l_321 = (void*)0; + int32_t *l_322 = &l_233; + l_320[3][0][0] = ((safe_mul_func_int16_t_s_s(((safe_unary_minus_func_uint8_t_u(((*l_310) &= p_88))) <= ((g_313[1][2][2] = (l_311 , l_312)) != (void*)0)), g_237)) == (((l_311 , (safe_rshift_func_uint16_t_u_u(((&g_282 == ((safe_mul_func_int16_t_s_s((safe_mul_func_uint16_t_u_u(g_5, g_82.f4)), 0x1FCCL)) , &g_71)) & 0xE3CFL), 6))) ^ p_88) , 0x06L)); + (*l_322) = p_88; + l_235 ^= l_311.f5; + if (p_88) + continue; + } + else + { /* block id: 191 */ + int16_t *l_324 = &l_124[4][2].f1; + int32_t *l_325 = &l_235; + int32_t l_326[6][8][5] = {{{(-1L),0L,0x77968830L,(-3L),0xF69D3694L},{0x80481311L,(-8L),0x6EE43BC7L,3L,0xF69D3694L},{9L,0xA3F52DB4L,0x332D84DCL,0x6EE43BC7L,2L},{0xB9E29AD6L,0x6EE43BC7L,8L,1L,0L},{0xD4BE4A73L,(-1L),0L,0x34B194B4L,1L},{0x332D84DCL,9L,0L,0L,9L},{0L,2L,0xC6B43B59L,1L,0xD4BE4A73L},{(-1L),8L,0xA5022890L,1L,(-1L)}},{{1L,0xB9E29AD6L,0xFF1388ADL,0x5D62FFCCL,(-4L)},{(-1L),0xA96E957DL,1L,0xC06D5091L,6L},{0L,0x075FB35CL,1L,8L,0x332D84DCL},{0x332D84DCL,0L,0xD4BE4A73L,0xA3F52DB4L,(-7L)},{0xD4BE4A73L,4L,(-1L),0x9E963DA1L,0x346217EBL},{0xB9E29AD6L,0xC3EFD975L,0x7C2A98AAL,0xAB407981L,0xC06D5091L},{9L,0x4A7388A2L,0xF9C56E4CL,(-7L),0x57D97B32L},{(-7L),0x3E1D9BA4L,0x093B7A13L,0x075FB35CL,(-4L)}},{{1L,0xAAC8C820L,2L,0x5D62FFCCL,0xA3F52DB4L},{1L,9L,0x5D62FFCCL,0xDAE7BCCAL,1L},{0xA96E957DL,(-4L),8L,0L,0L},{(-2L),0x4A7388A2L,(-2L),0xF69D3694L,0x9E963DA1L},{0xCA74A0EEL,1L,(-8L),1L,0L},{(-8L),0xA3F52DB4L,0x3E1D9BA4L,0x63D06EA8L,0x346217EBL},{(-4L),1L,(-8L),0L,(-1L)},{0L,(-3L),(-2L),(-1L),0x075FB35CL}},{{(-3L),0xF69D3694L,8L,1L,0x80481311L},{(-1L),9L,0x5D62FFCCL,(-1L),0x3E1D9BA4L},{9L,(-1L),2L,0x4A7388A2L,0xA96E957DL},{9L,0xC3EFD975L,0x093B7A13L,0x6EE43BC7L,1L},{0xA5022890L,0L,0xF9C56E4CL,0x6EE43BC7L,0xC3EFD975L},{1L,8L,0xA314B29BL,0x4A7388A2L,9L},{0L,0xAAA1835EL,1L,(-1L),0xAAC8C820L},{0x9E963DA1L,(-2L),(-1L),1L,0xB9E29AD6L}},{{(-3L),(-1L),0x34B194B4L,(-1L),(-3L)},{0xAAA1835EL,1L,4L,0xA314B29BL,3L},{0xAAA1835EL,0xDAE7BCCAL,0x57D97B32L,8L,0xAAC8C820L},{2L,(-1L),9L,0xF69D3694L,3L},{0xF553E666L,8L,0x075FB35CL,0xAAA1835EL,9L},{3L,0xD4BE4A73L,(-2L),(-3L),9L},{0xB9E29AD6L,0L,1L,0x9E963DA1L,0x6EE43BC7L},{1L,0xF553E666L,(-3L),0L,0xCA74A0EEL}},{{(-8L),(-1L),0xA3F52DB4L,1L,(-1L)},{0xA96E957DL,0L,1L,0xA5022890L,0x57D97B32L},{0xA96E957DL,1L,0xFF1388ADL,9L,(-7L)},{(-8L),(-1L),1L,9L,0x5D62FFCCL},{1L,0xA5022890L,8L,(-1L),1L},{0xB9E29AD6L,0L,1L,(-3L),1L},{3L,3L,(-1L),0L,(-4L)},{0xF553E666L,0x3E1D9BA4L,0xC3EFD975L,(-4L),1L}}}; + int32_t *l_327[3][8][3] = {{{&l_236,(void*)0,&l_233},{&l_299,&l_299,&g_39},{(void*)0,&l_163,(void*)0},{&g_39,&l_236,&l_163},{&l_236,&g_2,&l_299},{&g_39,&l_299,&l_233},{&g_39,&g_39,&g_39},{&l_236,&l_233,(void*)0}},{{&g_238,&l_236,&l_299},{&l_299,&l_233,&l_236},{&l_233,(void*)0,&l_299},{&g_238,&l_233,(void*)0},{&g_238,&l_236,&g_39},{&g_39,&g_2,&l_236},{&l_236,&g_238,&g_39},{&g_238,&l_236,&g_39}},{{&l_236,&l_236,&l_236},{&l_233,&g_237,&g_39},{&g_238,&l_163,(void*)0},{(void*)0,&g_238,&l_299},{&l_299,&g_238,&l_236},{(void*)0,(void*)0,&l_299},{&g_238,&g_2,(void*)0},{&l_233,&l_236,&g_238}}}; + int i, j, k; + l_311.f5 = p_88; + g_237 |= (((0xABB76B37L & ((0xAA467251L && l_323) | ((*l_325) = ((l_323 , ((*l_324) = p_88)) ^ p_88)))) < l_306) , (l_311.f6 = (l_326[5][3][0] &= ((*l_325) ^= p_88)))); + return g_280; + } + l_328 |= (~l_323); + } + } + else + { /* block id: 203 */ + int32_t l_330 = (-1L); + int32_t *l_331 = &l_235; + int32_t *l_332 = &g_39; + int32_t *l_333 = &g_238; + int32_t *l_334 = (void*)0; + int32_t *l_335 = &l_235; + int32_t *l_336 = &l_235; + int32_t *l_337 = &l_236; + int32_t *l_338 = &l_329; + int32_t *l_339[9]; + int i; + for (i = 0; i < 9; i++) + l_339[i] = &g_237; + ++g_347; + } + } + for (l_236 = (-7); (l_236 < (-27)); l_236 = safe_sub_func_int16_t_s_s(l_236, 6)) + { /* block id: 209 */ + int32_t l_368 = 0xAE9026B3L; + uint32_t l_369 = 0xF0C741AFL; + uint8_t *l_370 = &l_285; + int32_t l_397 = 0L; + struct S1 l_398 = {3,768,1037,163,753,24,-442,139,64,9UL}; + uint16_t l_411 = 65534UL; + uint16_t l_438 = 65528UL; + uint32_t l_486 = 0xC4A867A1L; + for (g_347 = 0; (g_347 > 19); g_347 = safe_add_func_uint8_t_u_u(g_347, 1)) + { /* block id: 212 */ + int16_t l_359 = 0xF62AL; + int32_t l_360[5][9][5] = {{{0x9DF86A6FL,0x95036420L,0x166F187FL,0x696BFCAAL,0x696BFCAAL},{1L,(-1L),1L,0x27CAFC2EL,1L},{0x94B78765L,0x9DF86A6FL,0x4426EC2FL,0x4E8F0221L,7L},{0xD0B67DE8L,0xEF7070EBL,1L,0L,0xF0132B51L},{0x9AFEC63DL,0x166F187FL,0x4426EC2FL,7L,(-9L)},{0L,1L,1L,1L,0L},{0xBC3A7848L,0x6A9B86FFL,0x166F187FL,0xF529EB06L,0x166F187FL},{0x27CAFC2EL,0x27CAFC2EL,(-3L),0x47BB2F77L,(-1L)},{0xDC3195D9L,(-1L),1L,0x94B78765L,(-1L)}},{{7L,0L,1L,0xC021C2B8L,0x47BB2F77L},{0x86921A87L,0x95036420L,(-1L),0x29DEC1BEL,0x6A9B86FFL},{0xEF7070EBL,0xD0B67DE8L,(-1L),(-3L),0x845B019AL},{(-1L),(-1L),0x9AFEC63DL,0x9AFEC63DL,(-1L)},{0L,0xC021C2B8L,0L,7L,1L},{0xBC3A7848L,0xDC3195D9L,0x8D604015L,0x6A9B86FFL,0x9AFEC63DL},{1L,(-1L),(-9L),(-1L),0xFFED5E11L},{0xBC3A7848L,0x4E8F0221L,8L,0x95036420L,1L},{0L,0L,0xF0132B51L,0x27CAFC2EL,7L}},{{(-1L),0x86921A87L,(-1L),0x86921A87L,(-1L)},{0xEF7070EBL,(-1L),0x27CAFC2EL,0x47BB2F77L,0xD0B67DE8L},{0x86921A87L,(-9L),7L,0x4426EC2FL,0x166F187FL},{(-1L),0xFFED5E11L,1L,(-1L),0xD0B67DE8L},{0xF529EB06L,0x4426EC2FL,0x94B78765L,0x696BFCAAL,(-1L)},{0xD0B67DE8L,(-1L),(-3L),0x845B019AL,7L},{0x8D604015L,0xA1B0B983L,0xA1B0B983L,0x8D604015L,1L},{0x27CAFC2EL,0xEBE5C5FEL,(-1L),0L,0xFFED5E11L},{0x166F187FL,0xBC3A7848L,0x86921A87L,6L,0x9AFEC63DL}},{{0x845B019AL,(-1L),7L,0L,1L},{0xA1B0B983L,0x4426EC2FL,(-9L),0x166F187FL,0x4E8F0221L},{0L,(-1L),(-1L),0x47BB2F77L,0x47BB2F77L},{0xA595A854L,6L,0xA595A854L,7L,0x9DF86A6FL},{0xD0B67DE8L,0L,0xFFED5E11L,0xEBE5C5FEL,0L},{(-1L),0xF529EB06L,0x9DF86A6FL,(-1L),0xC03EEE59L},{0xEF7070EBL,(-1L),0xFFED5E11L,0L,0xF0132B51L},{0xA1B0B983L,0x9DF86A6FL,0xA595A854L,(-9L),(-1L)},{0L,(-1L),(-1L),(-1L),(-1L)}},{{7L,7L,(-9L),0x29DEC1BEL,(-1L)},{0L,7L,1L,0xD0B67DE8L,(-1L)},{0x6A9B86FFL,8L,(-9L),0x9DF86A6FL,1L},{(-1L),7L,0L,1L,1L},{0xDC3195D9L,7L,0x696BFCAAL,0x8D604015L,(-1L)},{0L,(-1L),1L,1L,(-1L)},{0xC03EEE59L,0x9DF86A6FL,8L,0x86921A87L,(-9L)},{(-3L),(-1L),0L,1L,1L},{(-1L),0xF529EB06L,0x256ADFB4L,0x696BFCAAL,0x9AFEC63DL}}}; + struct S0 l_364 = {0UL,3L,0xA013L,0UL,6UL}; + int i, j, k; + for (l_235 = 0; (l_235 == (-7)); l_235--) + { /* block id: 215 */ + uint8_t l_361 = 0x8AL; + for (g_71 = 18; (g_71 >= 25); g_71 = safe_add_func_int32_t_s_s(g_71, 4)) + { /* block id: 218 */ + int32_t *l_358[9] = {&l_340,&l_340,&l_340,&l_340,&l_340,&l_340,&l_340,&l_340,&l_340}; + int32_t ***l_367[10][6][4] = {{{&g_365,&g_365,&g_365,(void*)0},{&g_365,&g_365,&g_365,&g_365},{&g_365,&g_365,(void*)0,&g_365},{&g_365,(void*)0,(void*)0,&g_365},{&g_365,&g_365,&g_365,&g_365},{&g_365,&g_365,(void*)0,(void*)0}},{{&g_365,&g_365,(void*)0,&g_365},{&g_365,&g_365,&g_365,&g_365},{&g_365,(void*)0,&g_365,(void*)0},{&g_365,&g_365,&g_365,&g_365},{&g_365,(void*)0,&g_365,&g_365},{&g_365,&g_365,&g_365,&g_365}},{{(void*)0,&g_365,&g_365,&g_365},{&g_365,&g_365,&g_365,&g_365},{&g_365,(void*)0,(void*)0,&g_365},{&g_365,&g_365,&g_365,(void*)0},{&g_365,(void*)0,(void*)0,&g_365},{&g_365,&g_365,&g_365,&g_365}},{{&g_365,&g_365,&g_365,(void*)0},{&g_365,&g_365,(void*)0,&g_365},{&g_365,&g_365,(void*)0,&g_365},{&g_365,(void*)0,&g_365,&g_365},{&g_365,&g_365,&g_365,&g_365},{&g_365,&g_365,(void*)0,(void*)0}},{{&g_365,&g_365,&g_365,&g_365},{&g_365,&g_365,&g_365,&g_365},{&g_365,&g_365,(void*)0,&g_365},{&g_365,&g_365,&g_365,(void*)0},{&g_365,&g_365,(void*)0,&g_365},{&g_365,&g_365,&g_365,&g_365}},{{(void*)0,&g_365,&g_365,&g_365},{&g_365,&g_365,&g_365,&g_365},{&g_365,&g_365,&g_365,&g_365},{(void*)0,&g_365,&g_365,&g_365},{(void*)0,&g_365,&g_365,&g_365},{&g_365,(void*)0,(void*)0,&g_365}},{{&g_365,&g_365,&g_365,&g_365},{(void*)0,&g_365,&g_365,&g_365},{(void*)0,&g_365,&g_365,&g_365},{&g_365,&g_365,&g_365,&g_365},{&g_365,&g_365,&g_365,&g_365},{(void*)0,&g_365,&g_365,&g_365}},{{&g_365,&g_365,(void*)0,&g_365},{&g_365,&g_365,&g_365,&g_365},{&g_365,&g_365,(void*)0,&g_365},{&g_365,&g_365,&g_365,&g_365},{&g_365,&g_365,&g_365,&g_365},{&g_365,&g_365,&g_365,&g_365}},{{&g_365,&g_365,&g_365,&g_365},{(void*)0,&g_365,&g_365,&g_365},{&g_365,&g_365,&g_365,&g_365},{&g_365,&g_365,&g_365,&g_365},{&g_365,&g_365,&g_365,&g_365},{(void*)0,&g_365,&g_365,&g_365}},{{&g_365,&g_365,&g_365,&g_365},{&g_365,&g_365,(void*)0,(void*)0},{&g_365,&g_365,&g_365,&g_365},{&g_365,&g_365,&g_365,&g_365},{&g_365,&g_365,&g_365,&g_365},{&g_365,&g_365,&g_365,&g_365}}}; + int i, j, k; + g_238 |= p_88; + ++l_361; + g_365 = (l_364 , g_365); + } + } + for (g_241 = 0; (g_241 <= 6); g_241 += 1) + { /* block id: 226 */ + for (l_102 = 0; (l_102 <= 6); l_102 += 1) + { /* block id: 229 */ + int i, j; + if (g_36[(l_102 + 1)]) + break; + l_368 &= l_346[l_102][g_241]; + if (l_346[l_102][g_241]) + continue; + } + } + } + if (((((((((*l_370) = (l_369 = l_285)) > (l_311.f1 = ((++(*l_101)) , 255UL))) && (safe_rshift_func_uint8_t_u_u(((safe_mul_func_int8_t_s_s(0x57L, l_377)) != l_341), p_88))) >= ((safe_sub_func_int8_t_s_s((safe_div_func_uint16_t_u_u((safe_mul_func_uint16_t_u_u((g_282++), p_88)), 0x055CL)), (((((l_344 != p_88) , 1UL) , p_88) == g_82.f4) == g_18.f2))) && 0L)) && l_386) != (-4L)) , p_88)) + { /* block id: 241 */ + struct S0 *l_387 = &g_82; + int32_t *l_394 = &g_281; + int32_t l_399 = 0x4D4B183AL; + struct S1 **l_405 = &g_390; + for (g_18.f4 = 0; (g_18.f4 <= 6); g_18.f4 += 1) + { /* block id: 244 */ + struct S0 **l_388 = (void*)0; + struct S0 **l_389 = &l_387; + int i, j; + (*l_389) = l_387; + g_390 = g_390; + if (l_346[g_18.f4][g_18.f4]) + break; + } + for (l_109 = 0; (l_109 <= 7); l_109 += 1) + { /* block id: 251 */ + int16_t *l_400 = (void*)0; + int16_t *l_401 = &l_102; + int32_t *l_402 = &g_237; + int32_t *l_403 = &l_235; + int i; + for (g_39 = 8; (g_39 >= 3); g_39 -= 1) + { /* block id: 254 */ + int i; + return g_36[l_109]; + } + (*l_403) = ((*l_402) = ((g_36[l_109] && (g_36[l_109] ^ (g_240 > ((((*l_401) = (((safe_mod_func_uint32_t_u_u((((((*g_365) = &l_306) != l_394) || (safe_sub_func_int8_t_s_s((l_397 | (l_398 , (p_88 != p_88))), l_399))) <= 0x84L), 4294967295UL)) >= p_88) , l_369)) > 65529UL) < p_88)))) , p_88)); + } + (*l_405) = l_404; + } + else + { /* block id: 263 */ + int32_t *l_406[1][5][7] = {{{&l_368,&l_299,&l_299,&l_368,&l_299,&l_299,&l_368},{&l_299,&l_368,&l_299,&l_299,&l_368,&l_299,&l_299},{&l_368,&l_368,&l_368,&l_368,&l_368,&l_368,&l_368},{&l_368,&l_299,&l_299,&l_368,&l_299,&l_299,&l_368},{&l_299,&l_368,&l_299,&l_299,&l_368,&l_299,&l_299}}}; + struct S0 l_468 = {0x0EDE6D6DL,0L,65528UL,0x4463L,0x0FL}; + int i, j, k; + g_408--; + --l_411; + if ((safe_rshift_func_int8_t_s_s(0xF8L, l_340))) + { /* block id: 266 */ + uint32_t l_426[4][9] = {{0x88400371L,0UL,1UL,3UL,4294967295UL,3UL,1UL,0UL,0x88400371L},{0x470FF04AL,0x74C575B2L,0xEF5E38AEL,1UL,0x457E5FE2L,0x08800094L,0UL,0x08800094L,4294967295UL},{0xEF5E38AEL,0UL,0UL,0xEF5E38AEL,0x03ED131BL,0x9A6CC948L,0x88400371L,4294967295UL,0x457E5FE2L},{0UL,4294967295UL,0UL,0x74C575B2L,0x74C575B2L,0UL,4294967295UL,0UL,3UL}}; + int32_t l_431[6]; + int i, j; + for (i = 0; i < 6; i++) + l_431[i] = 0x9A6743CEL; + (*g_220) = ((p_88 , p_88) , (*g_220)); + if (p_88) + { /* block id: 268 */ + int32_t l_430 = 0x675E34E6L; + int32_t l_432 = 0xD3699B92L; + l_430 &= (4294967295UL || (l_235 = (safe_add_func_int16_t_s_s(((safe_div_func_int32_t_s_s(((((safe_mod_func_uint16_t_u_u((safe_add_func_uint32_t_u_u(0x4C38B039L, p_88)), ((*l_101) |= g_391.f4))) < g_18.f4) || (safe_rshift_func_uint8_t_u_s(((*l_370) |= l_426[1][2]), l_427))) && p_88), (safe_mul_func_int16_t_s_s(l_311.f7, l_344)))) <= g_391.f9), 0xE822L)))); + --l_435; + g_238 = l_346[6][7]; + } + else + { /* block id: 275 */ + (*g_220) = (void*)0; + } + g_39 = (p_88 == (l_438 <= (l_398.f8 & l_439))); + if (l_377) + break; + } + else + { /* block id: 280 */ + uint8_t l_459 = 0x18L; + const int32_t l_469 = 0x91247D0DL; + int32_t l_483 = 0x6A87FF15L; + int32_t l_496 = 0xDE6CCBD7L; + int32_t l_497 = 0xDBFCC0BBL; + int32_t l_498 = 1L; + for (g_222 = 0; (g_222 != 11); g_222++) + { /* block id: 283 */ + int8_t l_446 = 0x78L; + int32_t l_455 = 0x838E2474L; + l_455 |= (safe_sub_func_uint16_t_u_u(g_18.f1, (l_446 > (((safe_mod_func_int32_t_s_s(l_449, (l_427 = ((safe_lshift_func_int16_t_s_s(((l_235 = (-9L)) | 0x39081B19L), 2)) , (safe_lshift_func_int16_t_s_u(l_454, 13)))))) , &l_449) != ((*l_268) = ((*l_404) , l_370)))))); + return p_88; + } + if (l_398.f4) + { /* block id: 290 */ + struct S0 *l_457 = &l_124[6][0]; + struct S0 **l_456 = &l_457; + int32_t l_458 = 0x418B64C8L; + (*l_456) = &g_18; + if (l_346[6][6]) + continue; + ++l_459; + } + else + { /* block id: 294 */ + int16_t l_472 = (-3L); + uint32_t *l_475 = &g_347; + l_483 = ((safe_lshift_func_uint8_t_u_u((safe_rshift_func_uint16_t_u_u(l_459, (p_88 || ((safe_rshift_func_uint8_t_u_s((l_468 , l_469), 6)) >= p_88)))), (safe_rshift_func_int8_t_s_u((((l_472 != (safe_div_func_uint32_t_u_u(((*l_475)--), (0x0376L && (g_71 = (safe_rshift_func_int8_t_s_s((safe_mod_func_int16_t_s_s(l_340, (l_341 = 2L))), 6))))))) & l_482) , (-1L)), 4)))) >= 0x7FL); + if (p_88) + break; + return p_88; + } + for (g_208 = (-24); (g_208 != 13); g_208 = safe_add_func_int32_t_s_s(g_208, 6)) + { /* block id: 304 */ + int32_t l_494[6][4] = {{0x899E298AL,(-3L),1L,0x5B286B25L},{0x899E298AL,1L,0x899E298AL,0xFE2F18EAL},{(-3L),0x5B286B25L,0xFE2F18EAL,0xFE2F18EAL},{1L,1L,2L,0x5B286B25L},{0x5B286B25L,(-3L),2L,(-3L)},{1L,0x899E298AL,0xFE2F18EAL,2L}}; + int32_t l_495[9] = {0x708011CBL,0x708011CBL,0x708011CBL,0x708011CBL,0x708011CBL,0x708011CBL,0x708011CBL,0x708011CBL,0x708011CBL}; + int i, j; + g_238 = (p_88 > ((p_88 ^ (l_346[0][4] | (l_449 || ((l_486 , (safe_add_func_int32_t_s_s(p_88, (((safe_div_func_uint32_t_u_u(0x786AA2D0L, ((safe_add_func_int8_t_s_s((&g_251 == (void*)0), p_88)) & 1L))) , l_483) < 4294967292UL)))) , l_493)))) < l_469)); + ++g_501; + } + if (l_398.f8) + goto lbl_504; + } + l_346[0][2] = (g_119 ^ 4294967295UL); + } + } + l_507[0] = &g_18; + for (g_2 = 0; (g_2 >= (-22)); g_2 = safe_sub_func_int16_t_s_s(g_2, 1)) + { /* block id: 316 */ + uint32_t l_518[6] = {0UL,0UL,0UL,0UL,0UL,0UL}; + int32_t l_527 = (-10L); + int32_t l_538 = (-3L); + int32_t l_539 = (-3L); + int32_t l_552[7][7][5] = {{{0xDD201F80L,8L,0xEBE46D86L,8L,0xDD201F80L},{0L,1L,0x4F27EFA9L,(-4L),(-4L)},{0x3ECFA0B6L,8L,0x3ECFA0B6L,0x023C9910L,0x3ECFA0B6L},{0L,(-4L),1L,1L,(-4L)},{0xDD201F80L,0x023C9910L,0xEBE46D86L,0x023C9910L,0xDD201F80L},{(-4L),1L,1L,(-4L),0L},{0x3ECFA0B6L,0x023C9910L,0x3ECFA0B6L,8L,0x3ECFA0B6L}},{{(-4L),(-4L),0x4F27EFA9L,1L,0L},{0xDD201F80L,8L,0xEBE46D86L,8L,0xDD201F80L},{0L,1L,0x4F27EFA9L,(-4L),(-4L)},{0x3ECFA0B6L,8L,0x3ECFA0B6L,0x023C9910L,0x3ECFA0B6L},{0L,(-4L),1L,1L,(-4L)},{0xDD201F80L,0x023C9910L,0xEBE46D86L,0x023C9910L,0xDD201F80L},{(-4L),1L,1L,(-4L),0L}},{{0x3ECFA0B6L,0x023C9910L,0x3ECFA0B6L,8L,0x3ECFA0B6L},{(-4L),(-4L),0x4F27EFA9L,1L,0L},{0xDD201F80L,8L,0xEBE46D86L,8L,0xDD201F80L},{0L,1L,0x4F27EFA9L,(-4L),(-4L)},{0x3ECFA0B6L,8L,0x3ECFA0B6L,0x023C9910L,0x3ECFA0B6L},{0L,(-4L),1L,1L,(-4L)},{0xDD201F80L,0x023C9910L,0xEBE46D86L,0x023C9910L,0xDD201F80L}},{{(-4L),1L,1L,(-4L),0L},{0x3ECFA0B6L,0x023C9910L,0x3ECFA0B6L,8L,0x3ECFA0B6L},{(-4L),(-4L),0x4F27EFA9L,1L,0L},{0xDD201F80L,8L,0xEBE46D86L,8L,0xDD201F80L},{0L,1L,0x4F27EFA9L,(-4L),(-4L)},{0x3ECFA0B6L,8L,0x3ECFA0B6L,0x023C9910L,0x3ECFA0B6L},{0L,(-4L),1L,1L,(-4L)}},{{0xDD201F80L,0x023C9910L,0xEBE46D86L,0x023C9910L,0xDD201F80L},{(-4L),1L,1L,(-4L),0L},{0x3ECFA0B6L,0x023C9910L,0x3ECFA0B6L,8L,0x3ECFA0B6L},{(-4L),(-4L),0x4F27EFA9L,1L,0L},{0xDD201F80L,8L,0xEBE46D86L,8L,0xDD201F80L},{0L,1L,0x4F27EFA9L,(-4L),(-4L)},{0x3ECFA0B6L,8L,0x3ECFA0B6L,0x023C9910L,0x3ECFA0B6L}},{{0L,(-4L),1L,1L,(-4L)},{0xDD201F80L,0x023C9910L,0xEBE46D86L,0x023C9910L,0xDD201F80L},{(-4L),1L,1L,(-4L),0L},{0x3ECFA0B6L,0x023C9910L,0x3ECFA0B6L,8L,0x3ECFA0B6L},{(-4L),(-4L),0x4F27EFA9L,1L,0L},{0xDD201F80L,8L,0xEBE46D86L,8L,0xDD201F80L},{0L,1L,0x4F27EFA9L,(-4L),(-4L)}},{{0x3ECFA0B6L,8L,0x3ECFA0B6L,0x023C9910L,0x3ECFA0B6L},{0L,(-4L),1L,1L,(-4L)},{0xDD201F80L,0x023C9910L,0xEBE46D86L,0x023C9910L,0xDD201F80L},{(-4L),1L,1L,(-4L),0L},{0x3ECFA0B6L,0x023C9910L,0x3ECFA0B6L,8L,0x3ECFA0B6L},{(-4L),(-4L),0x4F27EFA9L,1L,0L},{0xDD201F80L,8L,0xEBE46D86L,8L,0xDD201F80L}}}; + int i, j, k; + } + } + return g_508[3][3][4].f0; +} + + + + +/* ---------------------------------------- */ +int main (int argc, char* argv[]) +{ + int i, j, k; + int print_hash_value = 0; + if (argc == 2 && strcmp(argv[1], "1") == 0) print_hash_value = 1; + platform_main_begin(); + crc32_gentab(); + func_1(); + transparent_crc(g_2, "g_2", print_hash_value); + transparent_crc(g_5, "g_5", print_hash_value); + transparent_crc(g_18.f0, "g_18.f0", print_hash_value); + transparent_crc(g_18.f1, "g_18.f1", print_hash_value); + transparent_crc(g_18.f2, "g_18.f2", print_hash_value); + transparent_crc(g_18.f3, "g_18.f3", print_hash_value); + transparent_crc(g_18.f4, "g_18.f4", print_hash_value); + for (i = 0; i < 8; i++) + { + transparent_crc(g_36[i], "g_36[i]", print_hash_value); + if (print_hash_value) printf("index = [%d]\n", i); + + } + transparent_crc(g_39, "g_39", print_hash_value); + transparent_crc(g_40, "g_40", print_hash_value); + transparent_crc(g_71, "g_71", print_hash_value); + transparent_crc(g_79, "g_79", print_hash_value); + transparent_crc(g_82.f0, "g_82.f0", print_hash_value); + transparent_crc(g_82.f1, "g_82.f1", print_hash_value); + transparent_crc(g_82.f2, "g_82.f2", print_hash_value); + transparent_crc(g_82.f3, "g_82.f3", print_hash_value); + transparent_crc(g_82.f4, "g_82.f4", print_hash_value); + transparent_crc(g_119, "g_119", print_hash_value); + transparent_crc(g_195, "g_195", print_hash_value); + transparent_crc(g_208, "g_208", print_hash_value); + transparent_crc(g_209, "g_209", print_hash_value); + transparent_crc(g_222, "g_222", print_hash_value); + transparent_crc(g_237, "g_237", print_hash_value); + transparent_crc(g_238, "g_238", print_hash_value); + transparent_crc(g_240, "g_240", print_hash_value); + transparent_crc(g_241, "g_241", print_hash_value); + transparent_crc(g_251, "g_251", print_hash_value); + transparent_crc(g_279, "g_279", print_hash_value); + transparent_crc(g_280, "g_280", print_hash_value); + transparent_crc(g_281, "g_281", print_hash_value); + transparent_crc(g_282, "g_282", print_hash_value); + transparent_crc(g_347, "g_347", print_hash_value); + transparent_crc(g_391.f0, "g_391.f0", print_hash_value); + transparent_crc(g_391.f1, "g_391.f1", print_hash_value); + transparent_crc(g_391.f2, "g_391.f2", print_hash_value); + transparent_crc(g_391.f3, "g_391.f3", print_hash_value); + transparent_crc(g_391.f4, "g_391.f4", print_hash_value); + transparent_crc(g_391.f5, "g_391.f5", print_hash_value); + transparent_crc(g_391.f6, "g_391.f6", print_hash_value); + transparent_crc(g_391.f7, "g_391.f7", print_hash_value); + transparent_crc(g_391.f8, "g_391.f8", print_hash_value); + transparent_crc(g_391.f9, "g_391.f9", print_hash_value); + transparent_crc(g_408, "g_408", print_hash_value); + transparent_crc(g_433, "g_433", print_hash_value); + transparent_crc(g_434, "g_434", print_hash_value); + for (i = 0; i < 3; i++) + { + for (j = 0; j < 1; j++) + { + transparent_crc(g_499[i][j], "g_499[i][j]", print_hash_value); + if (print_hash_value) printf("index = [%d][%d]\n", i, j); + + } + } + transparent_crc(g_500, "g_500", print_hash_value); + transparent_crc(g_501, "g_501", print_hash_value); + for (i = 0; i < 9; i++) + { + for (j = 0; j < 4; j++) + { + for (k = 0; k < 7; k++) + { + transparent_crc(g_508[i][j][k].f0, "g_508[i][j][k].f0", print_hash_value); + transparent_crc(g_508[i][j][k].f1, "g_508[i][j][k].f1", print_hash_value); + transparent_crc(g_508[i][j][k].f2, "g_508[i][j][k].f2", print_hash_value); + transparent_crc(g_508[i][j][k].f3, "g_508[i][j][k].f3", print_hash_value); + transparent_crc(g_508[i][j][k].f4, "g_508[i][j][k].f4", print_hash_value); + if (print_hash_value) printf("index = [%d][%d][%d]\n", i, j, k); + + } + } + } + for (i = 0; i < 3; i++) + { + transparent_crc(g_537[i], "g_537[i]", print_hash_value); + if (print_hash_value) printf("index = [%d]\n", i); + + } + transparent_crc(g_540, "g_540", print_hash_value); + transparent_crc(g_541, "g_541", print_hash_value); + transparent_crc(g_542, "g_542", print_hash_value); + transparent_crc(g_553, "g_553", print_hash_value); + for (i = 0; i < 1; i++) + { + for (j = 0; j < 2; j++) + { + transparent_crc(g_555[i][j], "g_555[i][j]", print_hash_value); + if (print_hash_value) printf("index = [%d][%d]\n", i, j); + + } + } + transparent_crc(g_564, "g_564", print_hash_value); + transparent_crc(g_802, "g_802", print_hash_value); + for (i = 0; i < 5; i++) + { + for (j = 0; j < 1; j++) + { + for (k = 0; k < 8; k++) + { + transparent_crc(g_809[i][j][k], "g_809[i][j][k]", print_hash_value); + if (print_hash_value) printf("index = [%d][%d][%d]\n", i, j, k); + + } + } + } + transparent_crc(g_876, "g_876", print_hash_value); + transparent_crc(g_910, "g_910", print_hash_value); + transparent_crc(g_1052, "g_1052", print_hash_value); + for (i = 0; i < 9; i++) + { + transparent_crc(g_1105[i], "g_1105[i]", print_hash_value); + if (print_hash_value) printf("index = [%d]\n", i); + + } + transparent_crc(g_1109, "g_1109", print_hash_value); + transparent_crc(g_1113.f0, "g_1113.f0", print_hash_value); + transparent_crc(g_1113.f1, "g_1113.f1", print_hash_value); + transparent_crc(g_1113.f2, "g_1113.f2", print_hash_value); + transparent_crc(g_1113.f3, "g_1113.f3", print_hash_value); + transparent_crc(g_1113.f4, "g_1113.f4", print_hash_value); + transparent_crc(g_1113.f5, "g_1113.f5", print_hash_value); + transparent_crc(g_1113.f6, "g_1113.f6", print_hash_value); + transparent_crc(g_1113.f7, "g_1113.f7", print_hash_value); + transparent_crc(g_1113.f8, "g_1113.f8", print_hash_value); + transparent_crc(g_1113.f9, "g_1113.f9", print_hash_value); + transparent_crc(g_1169.f0, "g_1169.f0", print_hash_value); + transparent_crc(g_1169.f1, "g_1169.f1", print_hash_value); + transparent_crc(g_1169.f2, "g_1169.f2", print_hash_value); + transparent_crc(g_1169.f3, "g_1169.f3", print_hash_value); + transparent_crc(g_1169.f4, "g_1169.f4", print_hash_value); + for (i = 0; i < 1; i++) + { + for (j = 0; j < 8; j++) + { + transparent_crc(g_1192[i][j], "g_1192[i][j]", print_hash_value); + if (print_hash_value) printf("index = [%d][%d]\n", i, j); + + } + } + for (i = 0; i < 8; i++) + { + transparent_crc(g_1233[i], "g_1233[i]", print_hash_value); + if (print_hash_value) printf("index = [%d]\n", i); + + } + transparent_crc(g_1263, "g_1263", print_hash_value); + transparent_crc(g_1389, "g_1389", print_hash_value); + platform_main_end(crc32_context ^ 0xFFFFFFFFUL, print_hash_value); + return 0; +} + +/************************ statistics ************************* +XXX max struct depth: 1 +breakdown: + depth: 0, occurrence: 476 + depth: 1, occurrence: 23 +XXX total union variables: 0 + +XXX non-zero bitfields defined in structs: 9 +XXX zero bitfields defined in structs: 0 +XXX const bitfields defined in structs: 1 +XXX volatile bitfields defined in structs: 0 +XXX structs with bitfields in the program: 26 +breakdown: + indirect level: 0, occurrence: 12 + indirect level: 1, occurrence: 9 + indirect level: 2, occurrence: 4 + indirect level: 3, occurrence: 1 +XXX full-bitfields structs in the program: 0 +breakdown: +XXX times a bitfields struct's address is taken: 22 +XXX times a bitfields struct on LHS: 0 +XXX times a bitfields struct on RHS: 19 +XXX times a single bitfield on LHS: 7 +XXX times a single bitfield on RHS: 26 + +XXX max expression depth: 49 +breakdown: + depth: 1, occurrence: 388 + depth: 2, occurrence: 107 + depth: 3, occurrence: 10 + depth: 4, occurrence: 7 + depth: 5, occurrence: 4 + depth: 6, occurrence: 3 + depth: 7, occurrence: 1 + depth: 8, occurrence: 1 + depth: 9, occurrence: 2 + depth: 10, occurrence: 1 + depth: 11, occurrence: 3 + depth: 12, occurrence: 1 + depth: 13, occurrence: 3 + depth: 14, occurrence: 3 + depth: 15, occurrence: 4 + depth: 16, occurrence: 5 + depth: 17, occurrence: 2 + depth: 18, occurrence: 3 + depth: 19, occurrence: 2 + depth: 20, occurrence: 4 + depth: 22, occurrence: 2 + depth: 23, occurrence: 2 + depth: 25, occurrence: 2 + depth: 27, occurrence: 1 + depth: 28, occurrence: 1 + depth: 29, occurrence: 2 + depth: 31, occurrence: 1 + depth: 32, occurrence: 2 + depth: 33, occurrence: 1 + depth: 35, occurrence: 1 + depth: 37, occurrence: 1 + depth: 49, occurrence: 1 + +XXX total number of pointers: 386 + +XXX times a variable address is taken: 972 +XXX times a pointer is dereferenced on RHS: 166 +breakdown: + depth: 1, occurrence: 159 + depth: 2, occurrence: 7 +XXX times a pointer is dereferenced on LHS: 233 +breakdown: + depth: 1, occurrence: 230 + depth: 2, occurrence: 3 +XXX times a pointer is compared with null: 15 +XXX times a pointer is compared with address of another variable: 6 +XXX times a pointer is compared with another pointer: 15 +XXX times a pointer is qualified to be dereferenced: 7549 + +XXX max dereference level: 4 +breakdown: + level: 0, occurrence: 0 + level: 1, occurrence: 2327 + level: 2, occurrence: 127 + level: 3, occurrence: 3 + level: 4, occurrence: 4 +XXX number of pointers point to pointers: 107 +XXX number of pointers point to scalars: 257 +XXX number of pointers point to structs: 22 +XXX percent of pointers has null in alias set: 32.4 +XXX average alias set size: 1.65 + +XXX times a non-volatile is read: 1305 +XXX times a non-volatile is write: 763 +XXX times a volatile is read: 0 +XXX times read thru a pointer: 0 +XXX times a volatile is write: 0 +XXX times written thru a pointer: 0 +XXX times a volatile is available for access: 0 +XXX percentage of non-volatile access: 100 + +XXX forward jumps: 2 +XXX backward jumps: 8 + +XXX stmts: 376 +XXX max block depth: 5 +breakdown: + depth: 0, occurrence: 34 + depth: 1, occurrence: 38 + depth: 2, occurrence: 35 + depth: 3, occurrence: 67 + depth: 4, occurrence: 91 + depth: 5, occurrence: 111 + +XXX percentage a fresh-made variable is used: 17.1 +XXX percentage an existing variable is used: 82.9 +FYI: the random generator makes assumptions about the integer size. See platform.info for more details. +********************* end of statistics **********************/ + diff --git a/tests/fuzz/2.c.txt b/tests/fuzz/2.c.txt new file mode 100644 index 00000000..8ca5593f --- /dev/null +++ b/tests/fuzz/2.c.txt @@ -0,0 +1 @@ +checksum = E09CCEB0 diff --git a/tests/fuzz/3.c b/tests/fuzz/3.c new file mode 100644 index 00000000..b2dcac2e --- /dev/null +++ b/tests/fuzz/3.c @@ -0,0 +1,1043 @@ +/* + * This is a RANDOMLY GENERATED PROGRAM. + * + * Generator: csmith 2.2.0 + * Git version: 2751ded + * Options: --no-volatiles --no-math64 + * Seed: 1532285979 + */ + +#include "csmith.h" + + +static long __undefined; + +/* --- Struct/Union Declarations --- */ +union U0 { + int8_t f0; + const uint8_t f1; +}; + +union U1 { + int8_t * f0; +}; + +union U2 { + int32_t f0; + uint32_t f1; + uint8_t f2; + uint32_t f3; + int8_t * f4; +}; + +/* --- GLOBAL VARIABLES --- */ +static uint8_t g_23 = 0x67L; +static int32_t g_25 = 1L; +static uint16_t g_34 = 0x451DL; +static union U2 g_42 = {-1L}; +static int32_t g_61[8][6][4] = {{{0xE281B1D0L,8L,9L,0xC90ACEFCL},{0xC369029EL,8L,0x63978472L,0x55BBB785L},{0x23D12EF6L,(-1L),0x23D12EF6L,8L},{0x55BBB785L,0xE4FCD58FL,0L,1L},{0xDD4F9520L,0xBF567D71L,0x9117A540L,0xE4FCD58FL},{4L,0L,0x9117A540L,9L}},{{0xDD4F9520L,(-1L),0L,0L},{0x55BBB785L,(-1L),0x23D12EF6L,0xDD4F9520L},{0x23D12EF6L,0xDD4F9520L,0x63978472L,0L},{0xC369029EL,9L,9L,0xC369029EL},{0xE281B1D0L,6L,0x55BBB785L,0x70A6219FL},{0L,0x63978472L,0xE281B1D0L,8L}},{{4L,9L,(-1L),8L},{0x63978472L,1L,0xC90ACEFCL,(-1L)},{0x9117A540L,(-1L),0x63978472L,0xE281B1D0L},{0L,0x9117A540L,0xC35C241EL,0x55F5DDA3L},{0x3A29ADD9L,(-1L),0x5DD88BA5L,(-1L)},{0xE4FCD58FL,8L,9L,0xBF567D71L}},{{0x55BBB785L,0xDD4F9520L,0xBF567D71L,0x9117A540L},{0x55F5DDA3L,0x3A29ADD9L,8L,1L},{0x55F5DDA3L,4L,0xBF567D71L,0x55BBB785L},{0x55BBB785L,1L,9L,0L},{0xE4FCD58FL,0L,0x5DD88BA5L,8L},{0x3A29ADD9L,0x5DD88BA5L,0xC35C241EL,0xC35C241EL}},{{0L,0L,0x63978472L,0xDD4F9520L},{0x9117A540L,6L,0xC90ACEFCL,(-1L)},{0x63978472L,0xC369029EL,(-1L),0xC90ACEFCL},{4L,0xC369029EL,0xE281B1D0L,(-1L)},{0xC369029EL,6L,8L,0xDD4F9520L},{0L,0L,0x9117A540L,0xC35C241EL}},{{0xE281B1D0L,0x5DD88BA5L,1L,8L},{6L,0L,6L,0L},{8L,1L,0x3A29ADD9L,0x55BBB785L},{(-1L),4L,0x70A6219FL,1L},{0x23D12EF6L,0x3A29ADD9L,0x70A6219FL,0x9117A540L},{(-1L),0xBF567D71L,0x55BBB785L,(-1L)}},{{0x9117A540L,0x23D12EF6L,0xE4FCD58FL,4L},{0xE4FCD58FL,4L,0x3A29ADD9L,(-1L)},{0x63978472L,0L,0L,0x63978472L},{1L,1L,0x9117A540L,0L},{0xC35C241EL,0x3A29ADD9L,0x63978472L,0x23D12EF6L},{8L,8L,4L,0x23D12EF6L}},{{0L,0x3A29ADD9L,0xC369029EL,0L},{0L,1L,0L,0x63978472L},{0x70A6219FL,0L,0xE281B1D0L,(-1L)},{0x55BBB785L,4L,6L,4L},{(-1L),0x23D12EF6L,8L,(-1L)},{9L,0xBF567D71L,(-1L),0L}}}; +static int32_t *g_60 = &g_61[3][1][1]; +static int8_t g_70 = 1L; +static int8_t *g_69 = &g_70; +static int32_t g_78 = 1L; +static union U1 g_84 = {0}; +static int32_t **g_95 = &g_60; +static int32_t ***g_94[1] = {&g_95}; +static int8_t g_117 = 7L; +static union U0 g_163 = {0xE2L}; +static uint16_t g_165[7] = {0xB953L,0xB953L,0xB953L,0xB953L,0xB953L,0xB953L,0xB953L}; +static const uint8_t g_171[4][8][3] = {{{8UL,8UL,8UL},{1UL,1UL,1UL},{8UL,8UL,8UL},{1UL,1UL,1UL},{8UL,8UL,8UL},{1UL,1UL,1UL},{8UL,8UL,8UL},{1UL,1UL,1UL}},{{8UL,8UL,8UL},{1UL,1UL,1UL},{8UL,8UL,8UL},{1UL,1UL,1UL},{8UL,8UL,8UL},{1UL,1UL,1UL},{8UL,8UL,8UL},{1UL,1UL,1UL}},{{8UL,8UL,8UL},{1UL,1UL,1UL},{8UL,8UL,8UL},{1UL,1UL,1UL},{8UL,8UL,8UL},{1UL,1UL,1UL},{8UL,8UL,8UL},{1UL,1UL,1UL}},{{8UL,8UL,8UL},{1UL,1UL,1UL},{8UL,8UL,8UL},{1UL,1UL,1UL},{8UL,8UL,8UL},{1UL,1UL,1UL},{8UL,8UL,8UL},{1UL,1UL,1UL}}}; +static const uint8_t *g_170 = &g_171[2][3][0]; +static uint8_t g_218 = 0xB9L; +static int16_t g_234 = 1L; +static int16_t g_246 = 0x3B2BL; +static int16_t *g_248 = &g_246; +static int16_t *g_249 = &g_246; +static int32_t g_255 = (-8L); +static int16_t g_380 = 0L; +static int16_t g_387 = 1L; +static union U2 *g_397 = &g_42; +static union U2 **g_396 = &g_397; +static const int32_t *g_404 = &g_25; +static union U0 g_405[4][1] = {{{0xD5L}},{{0xD5L}},{{0xD5L}},{{0xD5L}}}; +static int8_t g_471 = (-2L); +static uint16_t **g_505 = (void*)0; +static const int32_t g_555 = 0x72195204L; +static uint16_t g_556[9] = {8UL,0x32E2L,8UL,8UL,0x32E2L,8UL,8UL,0x32E2L,8UL}; +static int32_t *g_588 = &g_61[3][1][2]; +static uint8_t *g_616 = &g_42.f2; +static union U1 *g_644 = &g_84; +static union U1 **g_643 = &g_644; + + +/* --- FORWARD DECLARATIONS --- */ +static int32_t func_1(void); +static uint8_t func_4(int8_t * p_5, uint16_t p_6, int8_t * p_7); +static int8_t * func_8(uint8_t p_9, const uint32_t p_10, const int8_t * p_11, union U0 p_12); +static const int8_t func_16(union U0 p_17, uint8_t p_18, int8_t * p_19, int8_t * p_20); +static union U0 func_21(uint32_t p_22); +static int8_t func_27(uint16_t p_28, int32_t * p_29, union U2 p_30, int32_t * const p_31, int32_t p_32); +static int32_t * func_37(const uint16_t p_38, union U2 p_39, int32_t * p_40, int32_t p_41); +static int32_t * func_43(uint8_t p_44, int8_t p_45, int8_t * p_46); +static uint16_t func_50(int32_t * p_51, uint16_t p_52, int8_t p_53, int32_t * p_54, union U2 p_55); +static int32_t * func_56(int8_t * p_57, int32_t * p_58); + + +/* --- FUNCTIONS --- */ +/* ------------------------------------------ */ +/* + * reads : g_23 g_34 g_42 g_60 g_61 g_42.f3 g_69 g_70 g_42.f2 g_78 g_94 g_117 g_95 g_163 g_163.f0 g_42.f1 g_218 g_170 g_171 g_163.f1 g_84 g_248 g_246 g_234 g_249 g_405 g_255 g_387 g_25 g_396 g_397 g_380 g_471 g_165 g_404 g_505 g_588 g_643 + * writes: g_25 g_34 g_60 g_78 g_61 g_117 g_170 g_70 g_163.f0 g_218 g_165 g_234 g_42.f1 g_404 g_255 g_387 g_396 g_471 g_42.f2 g_616 + */ +static int32_t func_1(void) +{ /* block id: 0 */ + int32_t l_13[8] = {1L,1L,1L,1L,1L,1L,1L,1L}; + uint16_t *l_33 = &g_34; + uint32_t l_47 = 0xF844BA92L; + int8_t *l_59 = (void*)0; + union U2 l_64 = {-8L}; + int32_t l_175[3][5] = {{0xEB591F11L,0xEB591F11L,(-9L),4L,0xE9867546L},{(-1L),1L,1L,(-1L),0xEB591F11L},{(-1L),4L,0L,0L,4L}}; + int32_t l_509[9][6] = {{0xDF92247FL,0x59DF3EF6L,0x59DF3EF6L,0xDF92247FL,0x59DF3EF6L,0x59DF3EF6L},{0xDF92247FL,0x59DF3EF6L,0x59DF3EF6L,0xDF92247FL,0x59DF3EF6L,0x59DF3EF6L},{0xDF92247FL,0x59DF3EF6L,0x59DF3EF6L,0xDF92247FL,0x59DF3EF6L,0x59DF3EF6L},{0xDF92247FL,0x59DF3EF6L,0x59DF3EF6L,0xDF92247FL,0x59DF3EF6L,0x59DF3EF6L},{0xDF92247FL,0x59DF3EF6L,0x59DF3EF6L,0xDF92247FL,0x59DF3EF6L,0x59DF3EF6L},{0xDF92247FL,0x59DF3EF6L,0x59DF3EF6L,0xDF92247FL,0x59DF3EF6L,0x59DF3EF6L},{0xDF92247FL,0x59DF3EF6L,0x59DF3EF6L,0xDF92247FL,0x59DF3EF6L,0x59DF3EF6L},{0xDF92247FL,0x59DF3EF6L,0x59DF3EF6L,0xDF92247FL,0x59DF3EF6L,0x59DF3EF6L},{0xDF92247FL,0x59DF3EF6L,0x59DF3EF6L,0xDF92247FL,0x59DF3EF6L,0x59DF3EF6L}}; + uint32_t l_528 = 6UL; + uint8_t *l_576[8] = {&g_23,&g_23,&g_23,&g_23,&g_23,&g_23,&g_23,&g_23}; + int32_t l_635 = 0x6AE51A9FL; + int i, j; + if (((safe_add_func_uint16_t_u_u((l_509[1][5] ^= (func_4(func_8(l_13[7], (((safe_mul_func_uint8_t_u_u((func_16(func_21(g_23), (((*g_69) = func_27((++(*l_33)), func_37(l_13[7], g_42, func_43(l_47, (safe_div_func_uint8_t_u_u((func_50(func_56(l_59, g_60), g_61[6][5][1], l_13[2], &g_61[3][1][1], l_64) <= l_64.f1), g_42.f3)), g_69), l_47), l_64, &l_13[1], l_175[0][2])) < l_13[7]), l_59, g_69) >= g_171[1][6][2]), 0x43L)) < l_64.f2) != 5UL), g_69, g_405[3][0]), l_64.f0, l_59) >= l_64.f1)), l_64.f3)) > 0x66CCL)) + { /* block id: 365 */ + int32_t l_524 = (-9L); + union U2 l_580 = {0xF3A458B5L}; + int8_t *l_581 = (void*)0; + int32_t l_589 = 0x34F78B18L; + uint32_t l_605 = 0xE76FF136L; + for (g_234 = (-16); (g_234 >= 13); g_234 = safe_add_func_int32_t_s_s(g_234, 6)) + { /* block id: 368 */ + union U2 l_520 = {0x03E6CC5CL}; + int32_t l_529 = (-7L); + int32_t *l_566 = &l_175[0][0]; + const uint8_t *l_577 = &g_171[2][4][1]; + uint32_t *l_584 = &l_580.f1; + uint8_t l_587 = 0x74L; + if (l_64.f1) + { /* block id: 369 */ + uint32_t l_516 = 0UL; + uint8_t *l_519 = &g_42.f2; + union U2 *l_521 = &l_64; + int32_t *l_525 = (void*)0; + int32_t *l_526 = &g_61[3][1][1]; + int32_t *l_527[7][2] = {{&l_13[4],&l_13[4]},{(void*)0,&l_13[4]},{&l_13[4],(void*)0},{&l_13[4],&l_13[4]},{(void*)0,&l_13[4]},{&l_13[4],(void*)0},{&l_13[4],&l_13[4]}}; + int16_t l_554 = 0xB066L; + uint16_t *l_571 = &g_165[6]; + int i, j; + l_529 = (safe_div_func_int16_t_s_s((safe_rshift_func_uint16_t_u_s(l_516, (safe_rshift_func_int8_t_s_u(l_516, ((*l_519) = 0x47L))))), (((*l_521) = l_520) , ((l_528 = ((*l_526) = (safe_lshift_func_uint16_t_u_s((func_16(g_163, l_524, l_519, &g_70) && 1L), 8)))) | l_520.f0)))); + if ((*l_526)) + break; + for (g_34 = 0; (g_34 <= 5); g_34 = safe_add_func_int16_t_s_s(g_34, 5)) + { /* block id: 378 */ + uint32_t l_534 = 0x9C38BE8CL; + int8_t **l_537 = &g_69; + int32_t *l_565 = &l_13[0]; + } + } + else + { /* block id: 394 */ + return l_175[0][1]; + } + l_589 = ((*g_588) = (safe_mod_func_uint16_t_u_u(((((l_576[2] = &g_218) == l_577) && (~(safe_mul_func_uint8_t_u_u((l_13[5] >= (l_580 , g_165[5])), (*l_566))))) > 0x0256L), l_47))); + (*g_588) ^= (*l_566); + } + for (g_117 = (-29); (g_117 != (-7)); g_117 = safe_add_func_int8_t_s_s(g_117, 5)) + { /* block id: 407 */ + int32_t *l_592 = &l_13[7]; + int32_t *l_593 = &l_13[0]; + int32_t *l_594 = &l_175[0][2]; + int32_t *l_595 = &l_509[1][5]; + int32_t *l_596 = &l_175[0][2]; + int32_t *l_597 = &l_509[1][5]; + int32_t *l_598 = &l_175[1][2]; + int32_t *l_599 = (void*)0; + int32_t *l_600 = &g_255; + int32_t *l_601 = &g_25; + int32_t *l_602 = &g_255; + int32_t l_603 = 0x83DE74D8L; + int32_t *l_604[5][7] = {{(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0},{&l_175[0][2],&g_61[3][1][0],&l_509[3][2],&g_61[3][1][0],&l_175[0][2],(void*)0,&l_175[0][2]},{(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,&g_255,(void*)0},{&l_509[3][2],&g_255,&l_509[3][2],(void*)0,&l_509[8][3],(void*)0,&l_509[3][2]},{(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0}}; + int i, j; + ++l_605; + } + } + else + { /* block id: 410 */ + int32_t *l_608[10] = {&l_175[0][2],&l_175[0][2],&l_175[0][2],&l_175[0][2],&l_175[0][2],&l_175[0][2],&l_175[0][2],&l_175[0][2],&l_175[0][2],&l_175[0][2]}; + uint8_t *l_612 = (void*)0; + uint8_t **l_613 = (void*)0; + uint8_t **l_614 = &l_612; + uint8_t **l_615[10] = {&l_576[5],&l_576[2],&l_576[5],&l_576[2],&l_576[5],&l_576[2],&l_576[5],&l_576[2],&l_576[5],&l_576[2]}; + uint32_t l_617 = 0xA732A71CL; + const int8_t *l_618 = &g_70; + int32_t *l_622 = (void*)0; + union U1 l_641 = {0}; + int i; + if (func_50(func_56(&g_70, l_608[8]), (safe_lshift_func_uint8_t_u_s(l_13[7], 7)), (((safe_unary_minus_func_int8_t_s(((*g_69) = ((*g_248) ^ (l_175[0][3] > ((g_616 = ((*l_614) = l_612)) != (l_576[4] = &g_23))))))) | l_64.f1) && g_165[4]), &l_175[0][0], (**g_396))) + { /* block id: 415 */ + return l_528; + } + else + { /* block id: 417 */ + int8_t l_619 = 0xF5L; + const int32_t *l_627[7][2] = {{&g_42.f0,&g_42.f0},{&g_42.f0,&g_42.f0},{&g_42.f0,&g_42.f0},{&l_64.f0,&l_64.f0},{&l_64.f0,&g_42.f0},{&g_42.f0,&g_42.f0},{&g_42.f0,&g_42.f0}}; + const int32_t **l_626 = &l_627[3][0]; + int32_t l_628[6] = {0xF3CEC250L,0xF3CEC250L,0xF3CEC250L,0xF3CEC250L,0xF3CEC250L,0xF3CEC250L}; + int16_t l_636 = 0xA7CDL; + uint8_t l_637 = 0x6EL; + int i, j; + for (g_387 = 7; (g_387 >= 0); g_387 -= 1) + { /* block id: 420 */ + int8_t *l_620[4][4][9] = {{{&g_163.f0,&l_619,(void*)0,&g_163.f0,&g_163.f0,(void*)0,&l_619,&g_163.f0,&g_405[3][0].f0},{&g_70,&g_471,(void*)0,&g_70,&l_619,&g_70,(void*)0,&g_117,&g_471},{&g_405[3][0].f0,&l_619,&g_163.f0,&g_471,(void*)0,&g_471,&g_163.f0,&l_619,&g_405[3][0].f0},{&g_163.f0,&g_471,&l_619,&g_70,&g_70,(void*)0,&g_471,(void*)0,&g_405[3][0].f0}},{{&g_163.f0,(void*)0,(void*)0,&g_163.f0,(void*)0,(void*)0,(void*)0,&g_163.f0,&g_163.f0},{&g_70,&g_405[3][0].f0,&g_70,&g_471,(void*)0,(void*)0,(void*)0,&g_471,&g_163.f0},{&l_619,(void*)0,&g_471,&g_163.f0,&g_163.f0,&g_117,&g_163.f0,&g_163.f0,&g_471},{&g_405[3][0].f0,&g_405[3][0].f0,&g_405[3][0].f0,&g_163.f0,&l_619,&g_117,&g_471,&g_70,&g_70}},{{&g_471,&g_163.f0,&l_619,&g_163.f0,(void*)0,&l_619,&l_619,(void*)0,&g_163.f0},{&g_405[3][0].f0,&g_117,&g_405[3][0].f0,(void*)0,&g_471,(void*)0,&g_70,&g_70,&l_619},{&g_117,&l_619,&g_471,&g_163.f0,(void*)0,&g_405[3][0].f0,&g_163.f0,&g_405[3][0].f0,(void*)0},{(void*)0,&g_70,&g_70,(void*)0,&g_117,(void*)0,&g_70,&l_619,&g_70}},{{&g_163.f0,&g_117,&g_163.f0,&g_163.f0,&l_619,&g_163.f0,&g_117,&g_117,&g_163.f0},{&g_117,&g_163.f0,(void*)0,&g_163.f0,&g_117,&l_619,&g_405[3][0].f0,&g_471,&g_70},{&l_619,&g_163.f0,&g_405[3][0].f0,&g_163.f0,(void*)0,&g_117,(void*)0,&g_163.f0,(void*)0},{&g_70,&l_619,&l_619,&l_619,&l_619,&g_70,(void*)0,&g_117,&g_70}}}; + int32_t l_629 = 0x94DF55BDL; + int32_t l_630 = 0x78F2A151L; + int32_t l_631 = 0x108DD9F6L; + int32_t l_632 = 1L; + int32_t l_633 = 0x9920FA05L; + int32_t l_634 = 8L; + int32_t *l_650 = &g_255; + int i, j, k; + for (g_163.f0 = 0; (g_163.f0 <= 3); g_163.f0 += 1) + { /* block id: 423 */ + int32_t *l_621[7][7] = {{&l_175[0][2],&g_61[3][4][2],&l_509[1][5],&l_175[0][2],&l_13[7],&l_175[0][2],&l_509[1][5]},{&g_255,&g_255,&l_13[7],&l_509[1][5],&g_61[3][4][2],(void*)0,&g_255},{&g_255,&l_509[1][5],&l_509[1][5],&g_61[3][4][2],&g_255,&g_255,&g_61[3][4][2]},{&l_175[0][2],&l_13[7],&l_175[0][2],&l_509[1][5],&g_61[3][4][2],&l_175[0][2],&l_175[0][2]},{&g_61[3][4][2],&l_13[7],&l_509[1][5],&l_509[1][5],&l_13[7],&l_13[7],&l_13[7]},{(void*)0,&l_509[1][5],&l_509[1][5],(void*)0,&l_175[0][2],&l_175[0][2],&g_61[3][4][2]},{&g_255,&g_255,&l_509[1][5],&l_509[1][5],&g_61[3][4][2],&g_255,&g_255}}; + const int32_t *l_624 = &g_78; + const int32_t **l_623[5]; + int i, j; + for (i = 0; i < 5; i++) + l_623[i] = &l_624; + for (g_78 = 3; (g_78 >= 0); g_78 -= 1) + { /* block id: 426 */ + int i, j, k; + g_61[(g_78 + 1)][(g_163.f0 + 1)][g_163.f0] ^= l_619; + (*g_95) = func_56(l_620[0][2][7], l_621[0][2]); + (*g_95) = (*g_95); + l_622 = l_621[0][2]; + } + for (l_619 = 1; (l_619 <= 7); l_619 += 1) + { /* block id: 434 */ + const int32_t ***l_625[2]; + int i; + for (i = 0; i < 2; i++) + l_625[i] = &l_623[1]; + l_626 = l_623[4]; + } + } + --l_637; + for (l_47 = 0; (l_47 <= 3); l_47 += 1) + { /* block id: 441 */ + int32_t l_640 = 0xEDD92DAAL; + int32_t *l_642 = (void*)0; + int8_t *l_649 = (void*)0; + (*g_588) |= (((g_165[2] = l_640) >= l_640) | ((((l_641 , (*g_170)) | (l_628[0] = (l_13[2] >= (func_50(l_642, (func_50(&l_628[2], (((g_643 == ((safe_rshift_func_uint8_t_u_s(l_628[2], 6)) , &g_644)) == 0x572EL) & 0L), l_634, l_608[7], (**g_396)) ^ 9UL), (*g_69), l_608[0], (*g_397)) || 0x10DDA243L)))) < l_13[7]) > l_13[g_387])); + for (l_636 = 0; (l_636 <= 3); l_636 += 1) + { /* block id: 447 */ + int32_t *l_651 = &l_628[2]; + l_650 = &l_628[2]; + (*g_95) = l_651; + if ((**g_95)) + break; + } + for (g_163.f0 = 5; (g_163.f0 >= 2); g_163.f0 -= 1) + { /* block id: 455 */ + int i, j, k; + g_61[g_387][l_47][l_47] = (safe_unary_minus_func_int16_t_s(l_509[(l_47 + 4)][l_47])); + l_509[3][0] ^= (*l_650); + return (*g_588); + } + for (l_528 = 2; (l_528 <= 7); l_528 += 1) + { /* block id: 462 */ + return (*g_404); + } + } + } + for (l_637 = 0; (l_637 == 3); l_637++) + { /* block id: 469 */ + uint32_t l_655[6]; + int i; + for (i = 0; i < 6; i++) + l_655[i] = 0x4CE808BAL; + (*g_95) = l_622; + if (l_655[4]) + continue; + } + } + } + for (g_218 = 3; (g_218 == 24); g_218++) + { /* block id: 477 */ + for (l_635 = 24; (l_635 <= 1); l_635 = safe_sub_func_int32_t_s_s(l_635, 2)) + { /* block id: 480 */ + (*g_588) = ((&g_78 == &g_78) ^ 0x108FL); + } + } + return (*g_588); +} + + +/* ------------------------------------------ */ +/* + * reads : g_387 g_95 g_84 g_61 g_170 g_171 g_163.f1 g_25 g_396 g_397 g_42 g_42.f3 g_117 g_248 g_246 g_255 g_380 g_471 g_249 g_165 g_163 g_69 g_70 g_42.f1 g_218 g_404 g_505 + * writes: g_387 g_60 g_404 g_218 g_25 g_117 g_396 g_255 g_61 g_471 g_170 g_34 + */ +static uint8_t func_4(int8_t * p_5, uint16_t p_6, int8_t * p_7) +{ /* block id: 299 */ + int32_t *l_430[6][9] = {{&g_61[3][1][1],&g_61[3][1][1],(void*)0,&g_61[1][1][0],&g_25,(void*)0,&g_25,&g_61[1][1][0],(void*)0},{&g_25,&g_25,&g_61[3][5][3],(void*)0,&g_61[5][0][1],&g_61[3][5][3],&g_61[5][0][1],(void*)0,&g_61[3][5][3]},{&g_61[3][1][1],&g_61[3][1][1],(void*)0,&g_61[1][1][0],&g_25,(void*)0,&g_25,&g_61[1][1][0],(void*)0},{&g_25,&g_25,&g_61[3][5][3],(void*)0,&g_61[5][0][1],&g_61[3][5][3],&g_61[5][0][1],(void*)0,&g_61[3][5][3]},{&g_61[3][1][1],&g_61[3][1][1],(void*)0,&g_61[3][1][1],&g_61[1][1][0],(void*)0,&g_61[1][1][0],&g_61[3][1][1],(void*)0},{&g_61[5][0][1],&g_61[5][0][1],&g_25,&g_25,(void*)0,&g_25,(void*)0,&g_25,&g_25}}; + uint32_t l_437[7][6] = {{0x73C76190L,0x73C76190L,1UL,0x73C76190L,0x73C76190L,1UL},{0x73C76190L,0x73C76190L,1UL,0x73C76190L,0x73C76190L,1UL},{0x73C76190L,0x73C76190L,1UL,0x73C76190L,0x73C76190L,1UL},{0x73C76190L,0x73C76190L,1UL,0x73C76190L,0x73C76190L,1UL},{0x73C76190L,0x73C76190L,1UL,0x73C76190L,0x73C76190L,1UL},{0x73C76190L,0x73C76190L,1UL,0x73C76190L,0x73C76190L,1UL},{0x73C76190L,0x73C76190L,1UL,0x73C76190L,0x73C76190L,1UL}}; + union U0 l_439 = {0x5BL}; + union U2 l_464 = {0x013712E9L}; + int8_t *l_468 = (void*)0; + union U2 l_470 = {0xFCE83381L}; + const union U1 l_504 = {0}; + int i, j; + for (g_387 = 10; (g_387 > (-15)); g_387--) + { /* block id: 302 */ + union U0 l_438[9][10] = {{{0x4FL},{0x2CL},{0x2CL},{0x4FL},{-6L},{0x4FL},{0x2CL},{0x2CL},{0x4FL},{-6L}},{{0x4FL},{0x2CL},{0x2CL},{0x4FL},{-6L},{0x4FL},{0x2CL},{0x2CL},{0x4FL},{-6L}},{{0x4FL},{0x2CL},{0x2CL},{0x4FL},{-6L},{0x4FL},{0x2CL},{0x2CL},{0x4FL},{-6L}},{{0x4FL},{0x2CL},{0x2CL},{0x4FL},{-6L},{0x4FL},{0x2CL},{0x6AL},{0x2CL},{-5L}},{{0x2CL},{0x6AL},{0x6AL},{0x2CL},{-5L},{0x2CL},{0x6AL},{0x6AL},{0x2CL},{-5L}},{{0x2CL},{0x6AL},{0x6AL},{0x2CL},{-5L},{0x2CL},{0x6AL},{0x6AL},{0x2CL},{-5L}},{{0x2CL},{0x6AL},{0x6AL},{0x2CL},{-5L},{0x2CL},{0x6AL},{0x6AL},{0x2CL},{-5L}},{{0x2CL},{0x6AL},{0x6AL},{0x2CL},{-5L},{0x2CL},{0x6AL},{0x6AL},{0x2CL},{-5L}},{{0x2CL},{0x6AL},{0x6AL},{0x2CL},{-5L},{0x2CL},{0x6AL},{0x6AL},{0x2CL},{-5L}}}; + int8_t *l_444 = &l_438[8][2].f0; + uint8_t *l_445 = (void*)0; + uint8_t *l_446 = (void*)0; + uint8_t *l_447 = (void*)0; + int32_t *l_448 = &g_25; + int i, j; + (*l_448) = func_50(((*g_95) = l_430[0][6]), (safe_lshift_func_uint8_t_u_u((safe_mod_func_int32_t_s_s((safe_mul_func_int16_t_s_s((l_437[5][3] , ((g_218 = func_16(l_438[8][2], func_16(l_439, (g_84 , func_16(l_439, (safe_rshift_func_uint8_t_u_s(0x01L, (safe_rshift_func_int16_t_s_s(0xFC91L, 13)))), p_7, l_444)), p_7, p_7), p_7, l_444)) ^ (*g_170))), g_387)), g_25)), 3)), l_438[8][2].f1, l_448, (**g_396)); + (*l_448) &= 9L; + if (p_6) + break; + } + for (g_117 = (-26); (g_117 < 23); g_117++) + { /* block id: 311 */ + int16_t l_469 = (-9L); + uint32_t *l_486 = &l_470.f1; + uint32_t **l_485[4] = {&l_486,&l_486,&l_486,&l_486}; + int8_t l_497 = 0L; + int32_t l_501 = 1L; + uint16_t l_508 = 0x4BC4L; + int i; + if (p_6) + { /* block id: 312 */ + int32_t *l_451[8] = {&g_255,&g_255,&g_255,&g_255,&g_255,&g_255,&g_255,&g_255}; + int32_t *l_452 = (void*)0; + const union U1 l_472 = {0}; + union U0 l_479 = {0xB8L}; + int i; + for (l_439.f0 = 3; (l_439.f0 >= 0); l_439.f0 -= 1) + { /* block id: 315 */ + union U2 ***l_453 = &g_396; + int32_t l_465 = 0xAF964579L; + uint16_t *l_482 = &g_165[0]; + l_452 = ((*g_95) = func_56(p_5, l_451[0])); + (*l_453) = &g_397; + for (g_387 = 5; (g_387 >= 0); g_387 -= 1) + { /* block id: 321 */ + union U0 l_454 = {8L}; + int i, j, k; + if (g_61[l_439.f0][g_387][l_439.f0]) + { /* block id: 322 */ + int32_t *l_463 = &g_255; + uint16_t l_466[9] = {0xBF85L,0xBF85L,0x789FL,0xBF85L,0xBF85L,0x789FL,0xBF85L,0xBF85L,0x789FL}; + uint16_t *l_467[1]; + int i, j, k; + for (i = 0; i < 1; i++) + l_467[i] = &g_165[4]; + g_61[(l_439.f0 + 3)][g_387][l_439.f0] = (func_16(l_454, (safe_rshift_func_uint16_t_u_u(((*l_463) ^= ((safe_sub_func_int16_t_s_s(((safe_sub_func_uint16_t_u_u(l_437[(l_439.f0 + 3)][(l_439.f0 + 2)], (l_466[1] = (func_16(l_454, (func_16(func_21(((*p_5) != (safe_sub_func_uint8_t_u_u((func_50(l_463, g_387, (*p_5), l_463, ((255UL == 0xC5L) , l_464)) || p_6), 1L)))), l_465, p_5, &g_70) == 0xC5F1L), p_5, p_5) ^ 0x5022C929L)))) != (-9L)), (*g_248))) ^ 0x01E9L)), g_380)), l_468, l_468) >= l_469); + } + else + { /* block id: 326 */ + (*l_452) ^= (-1L); + } + (*l_452) = ((g_84 , (((l_470 , (*g_170)) == g_471) ^ ((((func_50(&g_61[l_439.f0][g_387][l_439.f0], p_6, (g_61[l_439.f0][g_387][l_439.f0] = 0L), (l_472 , l_452), l_464) != 0UL) | 0x92L) | (*g_249)) > (*g_170)))) != (*g_249)); + } + for (g_471 = 0; (g_471 <= 3); g_471 += 1) + { /* block id: 334 */ + uint16_t *l_480 = (void*)0; + int32_t l_484 = 0x0E90AE7DL; + for (g_387 = 0; (g_387 <= 6); g_387 += 1) + { /* block id: 337 */ + uint16_t **l_481 = &l_480; + union U2 l_483 = {4L}; + int i, j, k; + (*g_95) = func_37((safe_rshift_func_int8_t_s_u(0xCBL, g_61[(l_439.f0 + 3)][(l_439.f0 + 1)][g_471])), (***l_453), &g_61[(l_439.f0 + 3)][(l_439.f0 + 1)][g_471], ((l_439 , ((safe_mod_func_uint32_t_u_u(((((*l_481) = (((safe_sub_func_int32_t_s_s(g_165[g_471], p_6)) , l_479) , l_480)) == l_482) & (*g_170)), p_6)) , l_483)) , l_484)); + } + for (l_465 = 3; (l_465 >= 0); l_465 -= 1) + { /* block id: 343 */ + int i, j, k; + return g_61[(g_471 + 1)][(l_439.f0 + 2)][l_465]; + } + } + } + return (*g_170); + } + else + { /* block id: 349 */ + uint32_t ***l_487 = (void*)0; + uint32_t ***l_488 = &l_485[3]; + int32_t l_495[9][9][2] = {{{0L,0x40CE7CD7L},{1L,0x40CE7CD7L},{0L,0x50ED6328L},{0xADA7BC83L,0x40CE7CD7L},{0L,0x40CE7CD7L},{0xADA7BC83L,0x50ED6328L},{0L,0x40CE7CD7L},{1L,0x40CE7CD7L},{0L,0x50ED6328L}},{{0xADA7BC83L,0x40CE7CD7L},{0L,0x40CE7CD7L},{0xADA7BC83L,0x50ED6328L},{0L,0x40CE7CD7L},{1L,0x40CE7CD7L},{0L,0x50ED6328L},{0xADA7BC83L,0x40CE7CD7L},{0L,0x40CE7CD7L},{0xADA7BC83L,0x50ED6328L}},{{0L,0x40CE7CD7L},{1L,0x40CE7CD7L},{0L,0x50ED6328L},{0xADA7BC83L,0x40CE7CD7L},{0L,0x40CE7CD7L},{0xADA7BC83L,0x50ED6328L},{0L,0x40CE7CD7L},{1L,0x40CE7CD7L},{0L,0x50ED6328L}},{{0xADA7BC83L,0x40CE7CD7L},{0L,0x40CE7CD7L},{0xADA7BC83L,0x50ED6328L},{0L,0x40CE7CD7L},{1L,0x40CE7CD7L},{0L,0x50ED6328L},{0xADA7BC83L,0x40CE7CD7L},{0L,0x40CE7CD7L},{0xADA7BC83L,0x50ED6328L}},{{0L,0x40CE7CD7L},{1L,0x40CE7CD7L},{0L,0x50ED6328L},{0xADA7BC83L,0x40CE7CD7L},{0L,0x40CE7CD7L},{0xADA7BC83L,0x50ED6328L},{0L,0x40CE7CD7L},{1L,0x40CE7CD7L},{0L,0x50ED6328L}},{{0xADA7BC83L,0x40CE7CD7L},{0L,0x40CE7CD7L},{0xADA7BC83L,0x50ED6328L},{0L,0x40CE7CD7L},{1L,0x40CE7CD7L},{0L,0x50ED6328L},{0xADA7BC83L,0x40CE7CD7L},{0L,0x40CE7CD7L},{0xADA7BC83L,0x50ED6328L}},{{0L,0x40CE7CD7L},{1L,0x40CE7CD7L},{0L,0x50ED6328L},{0xADA7BC83L,0x40CE7CD7L},{0L,0x50ED6328L},{1L,0xD0A14D25L},{0L,0x50ED6328L},{(-9L),0x50ED6328L},{0L,0xD0A14D25L}},{{1L,0x50ED6328L},{0x1F85E5ACL,0x50ED6328L},{1L,0xD0A14D25L},{0L,0x50ED6328L},{(-9L),0x50ED6328L},{0L,0xD0A14D25L},{1L,0x50ED6328L},{0x1F85E5ACL,0x50ED6328L},{1L,0xD0A14D25L}},{{0L,0x50ED6328L},{(-9L),0x50ED6328L},{0L,0xD0A14D25L},{1L,0x50ED6328L},{0x1F85E5ACL,0x50ED6328L},{1L,0xD0A14D25L},{0L,0x50ED6328L},{(-9L),0x50ED6328L},{0L,0xD0A14D25L}}}; + uint16_t *l_496 = &g_34; + int i, j, k; + (*l_488) = l_485[1]; + (*g_95) = func_37((p_6 ^ 0xE2F4L), (**g_396), l_430[0][6], ((safe_sub_func_uint32_t_u_u((safe_lshift_func_uint16_t_u_u(((*l_496) = (safe_mul_func_uint8_t_u_u(func_16(l_439, l_495[1][7][1], p_5, &g_70), 0UL))), g_25)), g_42.f1)) <= l_469)); + for (g_218 = 0; (g_218 <= 5); g_218 += 1) + { /* block id: 355 */ + uint32_t l_498[10][6][2] = {{{0UL,0x4710D315L},{0xC89510DFL,18446744073709551614UL},{18446744073709551615UL,0x32FB2497L},{18446744073709551615UL,0UL},{1UL,0xA19F4CA6L},{18446744073709551614UL,1UL}},{{0UL,1UL},{0x243448FFL,0x243448FFL},{18446744073709551615UL,18446744073709551614UL},{0x0D81DB8EL,0x78E1239EL},{1UL,0x4710D315L},{1UL,1UL}},{{0x32FB2497L,18446744073709551615UL},{0x32FB2497L,1UL},{1UL,0x4710D315L},{1UL,0x78E1239EL},{0x0D81DB8EL,18446744073709551614UL},{18446744073709551615UL,0x243448FFL}},{{0x243448FFL,1UL},{0UL,1UL},{18446744073709551614UL,0xA19F4CA6L},{1UL,0UL},{18446744073709551615UL,0x32FB2497L},{18446744073709551615UL,18446744073709551614UL}},{{0xC89510DFL,0x4710D315L},{0xFF073B46L,18446744073709551614UL},{0UL,1UL},{18446744073709551615UL,0UL},{0xF99EA8BEL,0xFF073B46L},{0x78E1239EL,18446744073709551615UL}},{{1UL,18446744073709551615UL},{0x78E1239EL,0xFF073B46L},{0xF99EA8BEL,0UL},{18446744073709551615UL,1UL},{0UL,18446744073709551614UL},{0xFF073B46L,0x53B970BDL}},{{0x78E1239EL,18446744073709551610UL},{0xC89510DFL,18446744073709551615UL},{0UL,0xFF073B46L},{0x76FB5125L,18446744073709551614UL},{18446744073709551610UL,1UL},{0UL,18446744073709551612UL}},{{0xF99EA8BEL,0xF99EA8BEL},{0UL,18446744073709551610UL},{0xA19F4CA6L,18446744073709551615UL},{18446744073709551612UL,0x53B970BDL},{0x76FB5125L,18446744073709551612UL},{18446744073709551615UL,0xC89510DFL}},{{18446744073709551615UL,18446744073709551612UL},{0x76FB5125L,0x53B970BDL},{18446744073709551612UL,18446744073709551615UL},{0xA19F4CA6L,18446744073709551610UL},{0UL,0xF99EA8BEL},{0xF99EA8BEL,18446744073709551612UL}},{{0UL,1UL},{18446744073709551610UL,18446744073709551614UL},{0x76FB5125L,0xFF073B46L},{0UL,18446744073709551615UL},{0xC89510DFL,18446744073709551610UL},{0x78E1239EL,0x53B970BDL}}}; + int i, j, k; + if ((*g_404)) + break; + l_498[0][0][0]--; + l_501 = l_498[0][0][0]; + } + } + l_501 &= (func_21((((~p_6) , (safe_div_func_int32_t_s_s(p_6, ((l_504 , 0L) | ((void*)0 == g_505))))) , (safe_rshift_func_int8_t_s_u(0x75L, l_508)))) , p_6); + } + return (*g_170); +} + + +/* ------------------------------------------ */ +/* + * reads : g_95 g_60 g_234 g_249 g_246 g_61 g_69 g_70 g_42.f3 g_42.f1 g_218 g_170 g_171 g_163.f1 g_34 g_163.f0 g_42 g_84 g_248 g_255 + * writes: g_60 g_70 g_78 g_34 g_163.f0 g_61 g_218 g_165 g_234 g_117 g_42.f1 g_255 + */ +static int8_t * func_8(uint8_t p_9, const uint32_t p_10, const int8_t * p_11, union U0 p_12) +{ /* block id: 293 */ + int8_t l_406[2][8] = {{0x4CL,0x4CL,(-6L),0x4CL,0x4CL,(-6L),0x4CL,0x4CL},{0x31L,0x4CL,0x31L,0x31L,0x4CL,0x31L,0x31L,0x4CL}}; + int32_t *l_407 = &g_61[1][3][0]; + int32_t *l_408 = (void*)0; + int32_t *l_409 = &g_61[3][1][1]; + int32_t *l_410 = &g_25; + int32_t *l_411 = (void*)0; + int32_t *l_412 = &g_25; + int32_t *l_413 = &g_61[3][1][1]; + int32_t *l_414 = &g_255; + int32_t *l_415 = &g_61[3][1][1]; + int32_t *l_416 = (void*)0; + int32_t l_417[3][4][5]; + int32_t *l_418[6][10][1] = {{{(void*)0},{(void*)0},{(void*)0},{&g_61[1][4][2]},{(void*)0},{(void*)0},{(void*)0},{(void*)0},{&g_61[1][4][2]},{(void*)0}},{{(void*)0},{(void*)0},{(void*)0},{(void*)0},{&g_25},{&g_25},{(void*)0},{(void*)0},{(void*)0},{(void*)0}},{{(void*)0},{(void*)0},{(void*)0},{&g_25},{&g_25},{(void*)0},{(void*)0},{(void*)0},{(void*)0},{(void*)0}},{{(void*)0},{(void*)0},{&g_25},{&g_25},{(void*)0},{(void*)0},{(void*)0},{(void*)0},{(void*)0},{(void*)0}},{{(void*)0},{&g_25},{&g_25},{(void*)0},{(void*)0},{(void*)0},{(void*)0},{(void*)0},{(void*)0},{(void*)0}},{{&g_25},{&g_25},{(void*)0},{(void*)0},{(void*)0},{(void*)0},{(void*)0},{(void*)0},{(void*)0},{&g_25}}}; + int16_t l_419 = 0x46F3L; + int32_t l_420 = 0xEFAEA218L; + int32_t l_421 = 0xF0C3CF87L; + int32_t l_422 = 0xF5496424L; + int8_t l_423 = (-7L); + uint32_t l_424 = 0xBE103C84L; + union U2 l_427 = {0x21442813L}; + int i, j, k; + for (i = 0; i < 3; i++) + { + for (j = 0; j < 4; j++) + { + for (k = 0; k < 5; k++) + l_417[i][j][k] = 0xF85FD576L; + } + } + l_406[1][3] ^= p_9; + (*g_95) = l_407; + l_424--; + (*l_414) &= func_27(p_9, (*g_95), l_427, (*g_95), ((&g_387 == (l_427 , &g_380)) != ((((p_12.f1 <= (0x201B5C96L > p_9)) >= g_234) ^ (*g_249)) , 0x9438L))); + return &g_117; +} + + +/* ------------------------------------------ */ +/* + * reads : g_61 + * writes: g_404 + */ +static const int8_t func_16(union U0 p_17, uint8_t p_18, int8_t * p_19, int8_t * p_20) +{ /* block id: 197 */ + uint16_t l_302[6][3] = {{1UL,1UL,1UL},{0UL,0UL,0UL},{1UL,1UL,1UL},{0UL,0UL,0UL},{1UL,1UL,1UL},{0UL,0UL,0UL}}; + int32_t *l_342[5][7][4] = {{{&g_255,&g_61[3][1][1],&g_61[2][3][3],&g_25},{&g_61[3][1][1],&g_25,&g_255,&g_255},{&g_255,&g_255,&g_255,&g_255},{&g_255,&g_255,&g_255,&g_255},{&g_255,&g_25,&g_61[3][1][1],&g_25},{&g_61[2][3][3],&g_61[3][1][1],&g_255,&g_61[3][1][1]},{&g_61[2][3][3],&g_255,&g_61[3][1][1],(void*)0}},{{&g_61[5][4][3],&g_255,&g_61[4][5][1],&g_25},{&g_255,(void*)0,&g_255,&g_25},{&g_61[4][5][1],&g_255,&g_61[5][4][3],(void*)0},{&g_61[2][3][3],&g_25,&g_255,&g_255},{&g_25,(void*)0,&g_255,&g_61[3][1][1]},{&g_61[2][3][3],&g_25,&g_61[5][4][3],&g_25},{&g_61[4][5][1],&g_25,&g_255,&g_25}},{{&g_255,&g_25,&g_61[4][5][1],&g_25},{&g_61[5][4][3],&g_25,&g_61[2][3][3],&g_61[3][1][1]},{&g_255,(void*)0,&g_25,&g_255},{&g_255,&g_25,&g_61[2][3][3],(void*)0},{&g_61[5][4][3],&g_255,&g_61[4][5][1],&g_25},{&g_255,(void*)0,&g_255,&g_25},{&g_61[4][5][1],&g_255,&g_61[5][4][3],(void*)0}},{{&g_61[2][3][3],&g_25,&g_255,&g_255},{&g_25,(void*)0,&g_255,&g_61[3][1][1]},{&g_61[2][3][3],&g_25,&g_61[5][4][3],&g_25},{&g_61[4][5][1],&g_25,&g_255,&g_25},{&g_255,&g_25,&g_61[4][5][1],&g_25},{&g_61[5][4][3],&g_25,&g_61[2][3][3],&g_61[3][1][1]},{&g_255,(void*)0,&g_25,&g_255}},{{&g_255,&g_25,&g_61[2][3][3],(void*)0},{&g_61[5][4][3],&g_255,&g_61[4][5][1],&g_25},{&g_255,(void*)0,&g_61[4][5][1],&g_255},{&g_61[3][1][1],&g_61[3][1][1],&g_25,&g_25},{&g_255,&g_61[3][1][1],&g_255,&g_61[3][1][1]},{&g_61[5][1][3],&g_25,&g_255,&g_255},{&g_255,(void*)0,&g_25,&g_61[3][1][1]}}}; + const union U2 *l_352 = &g_42; + uint16_t *l_377 = &g_165[1]; + uint16_t **l_376[4][4] = {{&l_377,(void*)0,&l_377,(void*)0},{&l_377,(void*)0,&l_377,(void*)0},{&l_377,(void*)0,&l_377,(void*)0},{&l_377,(void*)0,&l_377,(void*)0}}; + uint32_t l_393 = 4294967290UL; + const uint8_t *l_398 = &g_42.f2; + int8_t **l_401 = &g_69; + int8_t ***l_402 = &l_401; + const int32_t *l_403 = &g_61[3][1][1]; + int i, j, k; + for (p_18 = 25; (p_18 < 17); p_18 = safe_sub_func_uint32_t_u_u(p_18, 5)) + { /* block id: 200 */ + int32_t l_308 = 0xDC603431L; + int32_t l_309 = 0x1C33B4EEL; + union U1 *l_336 = &g_84; + int32_t l_388 = 4L; + uint8_t * const l_399 = &g_42.f2; + int8_t **l_400 = &g_69; + } + (*l_402) = l_401; + g_404 = (p_17.f1 , l_403); + return (*l_403); +} + + +/* ------------------------------------------ */ +/* + * reads : + * writes: g_25 + */ +static union U0 func_21(uint32_t p_22) +{ /* block id: 1 */ + int32_t *l_24 = &g_25; + union U0 l_26 = {-3L}; + (*l_24) = p_22; + return l_26; +} + + +/* ------------------------------------------ */ +/* + * reads : g_61 g_69 g_70 g_42.f3 g_42.f1 g_218 g_170 g_171 g_163.f1 g_95 g_34 g_163.f0 g_42 g_84 g_248 g_246 g_234 g_249 g_78 g_117 + * writes: g_70 g_78 g_34 g_163.f0 g_61 g_60 g_218 g_165 g_234 g_117 g_42.f1 + */ +static int8_t func_27(uint16_t p_28, int32_t * p_29, union U2 p_30, int32_t * const p_31, int32_t p_32) +{ /* block id: 106 */ + union U1 *l_177 = &g_84; + union U1 **l_176 = &l_177; + int32_t *l_178 = &g_61[3][1][1]; + int8_t *l_181[8] = {&g_163.f0,&g_163.f0,&g_163.f0,&g_163.f0,&g_163.f0,&g_163.f0,&g_163.f0,&g_163.f0}; + int32_t *l_182 = &g_61[3][1][1]; + union U2 l_252 = {8L}; + int32_t l_275 = 0x343AA78CL; + int32_t l_277 = 0xE7E876F0L; + int32_t l_289 = 0x3867B030L; + int32_t l_290 = 0x04B24578L; + int32_t l_291 = (-6L); + int32_t l_292 = 0xE153FDD8L; + int i; +lbl_256: + if (((+0x7E10L) ^ ((l_176 != (((*g_69) &= (*l_182)) , &l_177)) || (*l_178)))) + { /* block id: 108 */ + uint32_t l_193 = 18446744073709551606UL; + int32_t l_194 = 3L; + for (g_78 = 0; (g_78 <= (-16)); g_78 = safe_sub_func_int32_t_s_s(g_78, 2)) + { /* block id: 111 */ + int8_t l_192 = 0L; + for (g_34 = 0; (g_34 <= 7); g_34 += 1) + { /* block id: 114 */ + return (*l_178); + } + for (p_32 = 25; (p_32 == (-30)); p_32 = safe_sub_func_int32_t_s_s(p_32, 2)) + { /* block id: 119 */ + for (g_163.f0 = 0; (g_163.f0 != (-23)); g_163.f0 = safe_sub_func_int16_t_s_s(g_163.f0, 4)) + { /* block id: 122 */ + int8_t l_189 = (-3L); + for (g_34 = 0; (g_34 <= 3); g_34 += 1) + { /* block id: 125 */ + (*l_176) = &g_84; + } + if (l_189) + break; + } + if ((*l_178)) + break; + } + (*l_178) = (*p_31); + for (g_70 = 0; (g_70 < 3); ++g_70) + { /* block id: 135 */ + l_192 = 0x42813DB6L; + } + } + for (p_32 = 7; (p_32 >= 0); p_32 -= 1) + { /* block id: 141 */ + return p_30.f0; + } + l_194 = (~l_193); + return l_194; + } + else + { /* block id: 146 */ + int8_t l_197[10] = {1L,0x3CL,1L,(-1L),(-1L),1L,0x3CL,1L,(-1L),(-1L)}; + int32_t *l_198 = &g_61[3][1][1]; + union U0 l_199[1] = {{0xDFL}}; + int32_t *l_200 = (void*)0; + union U2 l_201[10] = {{0x8373BB44L},{0x8373BB44L},{0x8373BB44L},{0x8373BB44L},{0x8373BB44L},{0x8373BB44L},{0x8373BB44L},{0x8373BB44L},{0x8373BB44L},{0x8373BB44L}}; + uint16_t *l_224 = &g_34; + uint8_t *l_232 = (void*)0; + uint8_t ** const l_231 = &l_232; + union U1 l_240 = {0}; + int i; + if (((*l_178) = (safe_mul_func_uint16_t_u_u(((l_197[9] && func_50(l_198, (*l_182), (l_199[0] , p_30.f2), l_200, l_201[2])) >= (safe_mul_func_int8_t_s_s((*l_178), 0x2EL))), (*l_198))))) + { /* block id: 148 */ + return (*l_198); + } + else + { /* block id: 150 */ + uint8_t l_204 = 0xA3L; + uint32_t *l_217[3]; + union U2 l_223 = {0x3A80E33CL}; + uint16_t **l_225 = &l_224; + int i; + for (i = 0; i < 3; i++) + l_217[i] = &l_201[2].f1; +lbl_226: + l_204--; + if (g_42.f3) + goto lbl_256; + if (((*l_178) = (safe_sub_func_uint8_t_u_u((safe_add_func_uint16_t_u_u(g_42.f1, (g_165[6] = ((*l_198) >= ((safe_add_func_int32_t_s_s(0xBBEC7243L, (safe_lshift_func_int8_t_s_u(((((safe_mul_func_uint16_t_u_u(1UL, (*l_198))) < (++g_218)) && ((*g_170) ^ ((*g_69) = (safe_add_func_uint8_t_u_u(0UL, (l_223 , (((*l_225) = l_224) == &g_34))))))) == 0UL), (*g_170))))) , 0xACD3L))))), (*l_198))))) + { /* block id: 157 */ + return (*l_182); + } + else + { /* block id: 159 */ + int16_t *l_233 = &g_234; + int32_t l_235 = (-9L); + if (g_218) + goto lbl_226; + (*l_198) = (func_50(((*g_95) = l_217[0]), g_61[0][4][3], ((((safe_add_func_uint32_t_u_u(g_42.f3, (p_30 , (((*l_224) ^= (*l_182)) ^ g_163.f0)))) , (((*l_198) | ((((*l_233) = ((safe_add_func_uint16_t_u_u((((l_231 == &g_170) && 1L) != p_28), 0x6AFEL)) , p_30.f3)) , (*g_69)) > (*l_178))) ^ 0xE3L)) == (*g_69)) > l_235), l_178, g_42) , 0x23B8671DL); + } + } + for (g_117 = (-4); (g_117 <= 25); ++g_117) + { /* block id: 169 */ + int16_t *l_245 = &g_246; + int16_t **l_247[2]; + int32_t *l_250 = &g_61[3][1][1]; + int32_t l_253[6] = {0x4B38AC99L,0x4B38AC99L,0x12668D6FL,0x4B38AC99L,0x4B38AC99L,0x12668D6FL}; + int32_t *l_254 = &g_255; + int i; + for (i = 0; i < 2; i++) + l_247[i] = &l_245; + } + } + for (p_32 = 0; (p_32 > (-13)); p_32 = safe_sub_func_int32_t_s_s(p_32, 5)) + { /* block id: 181 */ + uint8_t l_264 = 0x45L; + int32_t l_272 = (-8L); + int32_t l_287[1][4] = {{0L,0L,0L,0L}}; + int16_t l_288 = (-1L); + uint32_t l_293 = 0xA6770327L; + int i, j; + for (p_28 = 1; (p_28 <= 7); p_28 += 1) + { /* block id: 184 */ + const union U1 *l_263[9] = {&g_84,&g_84,&g_84,&g_84,&g_84,&g_84,&g_84,&g_84,&g_84}; + int32_t l_265 = 0x3B5E6BF7L; + uint32_t *l_268 = &l_252.f1; + int16_t *l_271 = &g_234; + int32_t l_273 = 0x7167734CL; + int32_t *l_274 = (void*)0; + int32_t *l_276 = &g_255; + int32_t *l_278 = &l_273; + int32_t *l_279 = &g_255; + int32_t *l_280 = (void*)0; + int32_t *l_281 = &g_61[4][3][1]; + int32_t *l_282 = &l_275; + int32_t *l_283 = &l_265; + int32_t *l_284 = &l_275; + int32_t *l_285 = &l_277; + int32_t *l_286[9][2] = {{&l_275,&g_255},{&g_61[3][1][1],&g_61[3][1][1]},{&g_255,&l_275},{&g_61[4][1][1],&l_275},{&g_255,&g_61[3][1][1]},{&g_61[3][1][1],&g_255},{&l_275,&g_255},{&l_265,&l_275},{&g_255,&g_255}}; + int i, j; + l_273 |= ((p_32 ^ (p_32 != (safe_lshift_func_uint16_t_u_u(((safe_rshift_func_uint16_t_u_s((((void*)0 != l_263[4]) != ((*l_177) , (l_264 = ((g_42.f1 = p_28) | (*p_31))))), 14)) | l_265), ((safe_add_func_uint16_t_u_u(((++(*l_268)) , (((*l_271) ^= (*g_248)) <= l_272)), (*g_249))) || l_272))))) || 0x9B2CL); + if ((*l_182)) + continue; + l_293++; + (*l_182) |= l_272; + } + } + return (*l_182); +} + + +/* ------------------------------------------ */ +/* + * reads : g_117 g_163 g_61 g_69 g_70 g_42 g_42.f3 + * writes: g_170 g_61 g_60 + */ +static int32_t * func_37(const uint16_t p_38, union U2 p_39, int32_t * p_40, int32_t p_41) +{ /* block id: 101 */ + int32_t ***l_159 = &g_95; + int32_t l_160 = 0x1B0D62F1L; + uint16_t *l_164 = &g_165[4]; + const uint8_t *l_168 = &g_163.f1; + const uint8_t **l_169[5]; + const uint8_t l_172[4] = {0x82L,0x82L,0x82L,0x82L}; + int32_t *l_173 = &g_61[3][1][1]; + uint32_t l_174 = 18446744073709551614UL; + int i; + for (i = 0; i < 5; i++) + l_169[i] = &l_168; + (*l_173) = ((safe_div_func_uint8_t_u_u(p_39.f3, (safe_lshift_func_int16_t_s_u(g_117, ((((void*)0 != l_159) <= l_160) == (safe_lshift_func_int16_t_s_u((((g_163 , l_164) == &p_38) && (safe_lshift_func_uint16_t_u_s(((g_170 = l_168) != (void*)0), 0))), 6))))))) >= l_172[3]); + (*l_173) = ((*l_173) & ((func_50(p_40, ((*g_69) , l_174), (*g_69), &l_160, g_42) != (g_61[7][0][3] , p_39.f2)) <= 0xEEL)); + return p_40; +} + + +/* ------------------------------------------ */ +/* + * reads : g_69 g_70 g_61 g_60 g_42.f3 g_42.f2 g_78 g_94 g_23 g_117 g_95 + * writes: g_78 g_61 g_60 g_117 + */ +static int32_t * func_43(uint8_t p_44, int8_t p_45, int8_t * p_46) +{ /* block id: 15 */ + int32_t *l_71 = &g_61[3][1][1]; + int32_t *l_72 = (void*)0; + int32_t *l_73[9][8] = {{&g_61[3][1][1],(void*)0,(void*)0,&g_61[1][4][3],&g_61[0][3][1],&g_61[4][3][1],&g_61[6][4][2],&g_61[4][2][0]},{&g_61[2][4][2],(void*)0,(void*)0,&g_61[0][3][1],&g_61[0][3][1],(void*)0,(void*)0,&g_61[2][4][2]},{&g_61[3][1][1],&g_61[2][4][2],&g_61[3][1][2],&g_61[4][2][0],(void*)0,(void*)0,(void*)0,&g_61[1][2][0]},{(void*)0,(void*)0,(void*)0,(void*)0,&g_61[4][2][0],(void*)0,(void*)0,(void*)0},{&g_61[4][3][1],&g_61[2][4][2],&g_61[1][2][0],&g_61[3][0][0],&g_61[3][1][1],(void*)0,(void*)0,(void*)0},{(void*)0,(void*)0,&g_61[0][3][1],(void*)0,&g_61[4][3][1],&g_61[4][3][1],(void*)0,&g_61[0][3][1]},{(void*)0,(void*)0,&g_61[1][2][0],(void*)0,&g_61[1][4][3],&g_61[3][1][2],(void*)0,(void*)0},{&g_61[1][4][3],&g_61[3][1][2],(void*)0,(void*)0,(void*)0,&g_61[0][3][1],(void*)0,(void*)0},{&g_61[3][1][2],&g_61[1][2][0],&g_61[3][1][2],(void*)0,&g_61[6][4][2],(void*)0,(void*)0,&g_61[0][3][1]}}; + uint32_t l_74 = 0xFA7F9CD9L; + int32_t *l_77 = &g_78; + int32_t l_79 = 1L; + union U1 *l_86[5]; + int32_t ***l_96 = &g_95; + int8_t l_106 = 8L; + int8_t l_119[6] = {9L,0L,9L,9L,0L,9L}; + union U2 l_120 = {-5L}; + int32_t *l_134 = &g_61[3][1][1]; + int i, j; + for (i = 0; i < 5; i++) + l_86[i] = &g_84; +lbl_87: + l_74--; + l_79 ^= (((*l_77) = ((*g_69) || ((*l_71) < 0x14A1L))) , (*g_60)); + if ((g_42.f3 <= (1UL & (0xF049L <= g_42.f2)))) + { /* block id: 19 */ + union U1 *l_83 = &g_84; + int32_t l_90 = 0L; + for (p_45 = 0; (p_45 < (-22)); p_45--) + { /* block id: 22 */ + int32_t *l_88 = &g_61[3][1][1]; + for (l_79 = 6; (l_79 >= 0); l_79 -= 1) + { /* block id: 25 */ + int i, j; + l_73[(l_79 + 1)][l_79] = l_73[l_79][(l_79 + 1)]; + if (p_45) + { /* block id: 27 */ + const int16_t l_82 = (-10L); + for (g_78 = 7; (g_78 >= 0); g_78 -= 1) + { /* block id: 30 */ + union U1 **l_85[4] = {(void*)0,(void*)0,(void*)0,(void*)0}; + int i; + (*g_60) = l_82; + l_86[4] = ((p_45 <= g_70) , l_83); + } + if (g_42.f3) + goto lbl_87; + return l_88; + } + else + { /* block id: 36 */ + int32_t l_89 = 1L; + int i, j; + l_73[(l_79 + 1)][(l_79 + 1)] = (l_89 , l_73[l_79][(l_79 + 1)]); + if (p_44) + break; + } + } + if (l_90) + break; + (*l_71) |= (safe_lshift_func_int8_t_s_s(l_90, 2)); + } + } + else + { /* block id: 44 */ + int32_t l_93 = 0x63BE35A8L; + int32_t l_98 = (-9L); + int32_t l_99 = 0xE0221BC3L; + int32_t l_100 = 0xE0F627EEL; + int32_t l_103 = 7L; + int32_t l_104 = 0xBAAC5CA8L; + int32_t l_105 = 0xA126CF24L; + union U2 l_115 = {4L}; + int32_t *l_128 = &l_93; + int32_t *l_133 = &l_100; + if ((((l_93 , g_94[0]) != l_96) <= 5L)) + { /* block id: 45 */ + int32_t l_97 = 0xA0849DA6L; + int32_t l_101 = 1L; + int32_t l_102[5] = {(-1L),(-1L),(-1L),(-1L),(-1L)}; + uint8_t l_107 = 8UL; + int32_t *l_112 = &l_99; + uint8_t *l_116[10][4][4] = {{{&l_107,&l_107,&g_23,&l_115.f2},{&g_42.f2,&g_23,(void*)0,&l_107},{&g_42.f2,(void*)0,&g_23,&g_42.f2},{&l_107,&l_115.f2,(void*)0,&l_115.f2}},{{(void*)0,&l_115.f2,(void*)0,&g_42.f2},{&l_107,(void*)0,(void*)0,&l_107},{&l_107,&g_23,(void*)0,&l_115.f2},{&l_107,&l_107,(void*)0,(void*)0}},{{(void*)0,(void*)0,(void*)0,(void*)0},{&l_107,&l_107,&g_23,&l_115.f2},{&g_42.f2,&g_23,(void*)0,&l_107},{&g_42.f2,(void*)0,&g_23,&g_42.f2}},{{&l_107,&l_115.f2,(void*)0,&l_115.f2},{(void*)0,&l_115.f2,(void*)0,&g_42.f2},{&l_107,(void*)0,(void*)0,&l_107},{&l_107,&g_23,(void*)0,&l_115.f2}},{{&l_107,&l_107,(void*)0,(void*)0},{(void*)0,(void*)0,(void*)0,(void*)0},{&l_107,&l_107,&g_23,&l_115.f2},{&g_42.f2,&g_23,(void*)0,&l_107}},{{&g_42.f2,(void*)0,&g_23,&g_42.f2},{&l_107,&l_115.f2,(void*)0,&l_115.f2},{(void*)0,&l_115.f2,(void*)0,&g_42.f2},{&l_107,(void*)0,(void*)0,&g_42.f2}},{{&l_115.f2,&l_107,(void*)0,&g_23},{&g_42.f2,&l_115.f2,&l_107,&l_107},{&l_107,(void*)0,&g_23,&l_107},{&l_115.f2,&l_115.f2,(void*)0,&g_23}},{{(void*)0,&l_107,&l_107,&g_42.f2},{(void*)0,(void*)0,(void*)0,(void*)0},{&l_115.f2,&g_23,&g_23,&g_23},{&l_107,&g_23,&l_107,(void*)0}},{{&g_42.f2,(void*)0,(void*)0,&g_42.f2},{&l_115.f2,&l_107,(void*)0,&g_23},{&g_42.f2,&l_115.f2,&l_107,&l_107},{&l_107,(void*)0,&g_23,&l_107}},{{&l_115.f2,&l_115.f2,(void*)0,&g_23},{(void*)0,&l_107,&l_107,&g_42.f2},{(void*)0,(void*)0,(void*)0,(void*)0},{&l_115.f2,&g_23,&g_23,&g_23}}}; + int8_t *l_118 = &l_106; + union U2 l_121 = {0xF3683EEDL}; + uint32_t l_123 = 0x382ABCCDL; + int32_t *l_131 = &l_104; + int i, j, k; + l_107--; + l_101 &= ((safe_mod_func_int8_t_s_s((*p_46), (-1L))) == func_50(l_112, l_99, (safe_sub_func_int16_t_s_s(((0xD7L ^ (func_50(&l_99, g_42.f3, (((*l_118) = ((((g_117 |= (func_50(&l_102[2], (g_78 , g_23), (*g_69), &l_100, l_115) > g_42.f2)) , p_45) || 0x8DL) <= (-1L))) , l_119[2]), &l_101, l_120) , l_115.f2)) && 65535UL), 0x1D2AL)), &l_93, l_121)); + if (p_44) + { /* block id: 50 */ + int8_t l_122 = 1L; + union U2 *l_127[5] = {(void*)0,(void*)0,(void*)0,(void*)0,(void*)0}; + union U2 **l_126 = &l_127[3]; + int32_t *l_129 = &l_102[2]; + int32_t *l_130 = &l_104; + int32_t *l_132 = &l_101; + int i; + l_123--; + (*l_126) = &g_42; + return l_134; + } + else + { /* block id: 54 */ + uint16_t l_135 = 65535UL; + int32_t l_138 = (-1L); + if ((0L == 1L)) + { /* block id: 55 */ + for (l_98 = 5; (l_98 >= 1); l_98 -= 1) + { /* block id: 58 */ + int i; + if (l_119[l_98]) + break; + } + ++l_135; + } + else + { /* block id: 62 */ + int8_t l_142[10][6][4] = {{{0x71L,(-2L),0xA5L,0x84L},{(-9L),(-2L),0x0DL,1L},{0x80L,0L,0x3EL,5L},{0x71L,1L,9L,1L},{0x97L,(-4L),0x4CL,0x5CL},{0x45L,0x38L,0x71L,0xF5L}},{{0xA5L,0xA5L,1L,1L},{0L,0L,0x97L,(-1L)},{0x85L,0xBBL,0xE4L,0x3EL},{6L,0L,1L,0xE4L},{(-8L),0L,5L,0x3EL},{0L,0xBBL,(-1L),(-1L)}},{{0x4CL,0L,(-1L),1L},{0x84L,0xA5L,(-2L),0x71L},{(-1L),0x6FL,6L,8L},{1L,0x9DL,0x83L,0xFBL},{0L,0xDAL,(-2L),0L},{0x85L,0xF9L,1L,0xDAL}},{{0x18L,7L,0x4CL,0x83L},{0x78L,0x16L,(-2L),0x3EL},{0L,0x6DL,0x72L,0L},{1L,1L,(-1L),(-1L)},{0L,0x97L,(-2L),(-8L)},{0xA4L,(-1L),0x18L,0xA4L}},{{0x4CL,0xA5L,0x25L,0x97L},{1L,0xDAL,5L,0x31L},{9L,1L,0x83L,0x38L},{6L,0x84L,0x0DL,0x97L},{0x78L,1L,0x97L,0xDAL},{7L,(-1L),0x6FL,(-1L)}},{{0x1CL,(-8L),0x34L,0xB9L},{0x3EL,0x16L,1L,0x16L},{7L,0x34L,(-1L),0x31L},{0x8BL,(-1L),0x6FL,(-1L)},{(-1L),0x9DL,(-3L),1L},{(-1L),0L,0x6FL,0x78L}},{{0x8BL,1L,(-1L),9L},{7L,0L,1L,0x71L},{0x3EL,0x0DL,0x34L,0x85L},{1L,8L,0x0DL,0x6DL},{0x3EL,1L,(-7L),7L},{0x84L,1L,0x8BL,0x31L}},{{(-1L),0x16L,(-1L),0xBBL},{0L,0x97L,0x15L,0x38L},{0x97L,1L,0x6FL,0L},{0xE4L,0x3EL,0L,0x6DL},{0L,0x38L,0x5CL,0x78L},{0L,0x0DL,0L,0x03L}},{{0L,0x39L,0x18L,9L},{0x3EL,0L,(-9L),0xF8L},{0x84L,0L,0xE4L,0L},{0L,1L,0x6DL,(-1L)},{0xA4L,(-8L),(-9L),0x38L},{0xA5L,0x34L,(-1L),0x97L}},{{0x15L,1L,(-7L),0x1CL},{0L,(-1L),9L,0L},{(-1L),0x80L,0x80L,(-1L)},{1L,0x4CL,0x0BL,(-4L)},{1L,0L,0x85L,0xE4L},{(-1L),0x4FL,0x15L,0xE4L}}}; + int i, j, k; + if (((*g_60) = (**g_95))) + { /* block id: 64 */ + (*l_134) = (p_44 , func_50((**l_96), (*l_133), l_135, (**l_96), l_120)); + l_138 = ((l_133 != (void*)0) & p_45); + } + else + { /* block id: 67 */ + int32_t *l_141[6] = {&l_102[1],&l_102[1],&l_102[1],&l_102[1],&l_102[1],&l_102[1]}; + int i; + (*l_131) &= (*l_134); + (***l_96) = ((safe_rshift_func_uint8_t_u_s((p_44 = (((void*)0 == l_141[2]) , (l_142[0][3][1] = (*l_112)))), (*g_69))) || (-8L)); + (*l_134) |= p_44; + } + } + for (l_107 = 0; (l_107 <= 37); l_107++) + { /* block id: 77 */ + (**g_95) = (((p_44++) , (&g_95 == (void*)0)) & (*g_69)); + for (l_93 = 0; l_93 < 6; l_93 += 1) + { + l_119[l_93] = 0xC6L; + } + } + for (l_100 = 4; (l_100 >= 0); l_100 -= 1) + { /* block id: 84 */ + int32_t *l_147 = &l_104; + int32_t *l_148 = (void*)0; + return l_148; + } + (*g_95) = (**l_96); + } + for (g_117 = 0; (g_117 != (-14)); g_117--) + { /* block id: 91 */ + uint32_t l_151 = 0x6CEBD2D5L; + int32_t l_154 = 0x9DA40672L; + ++l_151; + l_154 = (p_45 >= (*l_133)); + } + } + else + { /* block id: 95 */ + return (*g_95); + } + } + (*l_71) |= (-1L); + return l_73[2][6]; +} + + +/* ------------------------------------------ */ +/* + * reads : g_42.f3 + * writes: g_60 + */ +static uint16_t func_50(int32_t * p_51, uint16_t p_52, int8_t p_53, int32_t * p_54, union U2 p_55) +{ /* block id: 8 */ + for (p_53 = 0; (p_53 < (-11)); p_53 = safe_sub_func_uint8_t_u_u(p_53, 9)) + { /* block id: 11 */ + int32_t **l_67 = (void*)0; + int32_t **l_68 = &g_60; + (*l_68) = p_54; + } + return g_42.f3; +} + + +/* ------------------------------------------ */ +/* + * reads : + * writes: + */ +static int32_t * func_56(int8_t * p_57, int32_t * p_58) +{ /* block id: 5 */ + int32_t **l_62 = (void*)0; + int32_t ***l_63 = &l_62; + (*l_63) = l_62; + return p_58; +} + + + + +/* ---------------------------------------- */ +int main (int argc, char* argv[]) +{ + int i, j, k; + int print_hash_value = 0; + if (argc == 2 && strcmp(argv[1], "1") == 0) print_hash_value = 1; + platform_main_begin(); + crc32_gentab(); + func_1(); + transparent_crc(g_23, "g_23", print_hash_value); + transparent_crc(g_25, "g_25", print_hash_value); + transparent_crc(g_34, "g_34", print_hash_value); + transparent_crc(g_42.f2, "g_42.f2", print_hash_value); + for (i = 0; i < 8; i++) + { + for (j = 0; j < 6; j++) + { + for (k = 0; k < 4; k++) + { + transparent_crc(g_61[i][j][k], "g_61[i][j][k]", print_hash_value); + if (print_hash_value) printf("index = [%d][%d][%d]\n", i, j, k); + + } + } + } + transparent_crc(g_70, "g_70", print_hash_value); + transparent_crc(g_78, "g_78", print_hash_value); + transparent_crc(g_117, "g_117", print_hash_value); + transparent_crc(g_163.f0, "g_163.f0", print_hash_value); + transparent_crc(g_163.f1, "g_163.f1", print_hash_value); + for (i = 0; i < 7; i++) + { + transparent_crc(g_165[i], "g_165[i]", print_hash_value); + if (print_hash_value) printf("index = [%d]\n", i); + + } + for (i = 0; i < 4; i++) + { + for (j = 0; j < 8; j++) + { + for (k = 0; k < 3; k++) + { + transparent_crc(g_171[i][j][k], "g_171[i][j][k]", print_hash_value); + if (print_hash_value) printf("index = [%d][%d][%d]\n", i, j, k); + + } + } + } + transparent_crc(g_218, "g_218", print_hash_value); + transparent_crc(g_234, "g_234", print_hash_value); + transparent_crc(g_246, "g_246", print_hash_value); + transparent_crc(g_255, "g_255", print_hash_value); + transparent_crc(g_380, "g_380", print_hash_value); + transparent_crc(g_387, "g_387", print_hash_value); + for (i = 0; i < 4; i++) + { + for (j = 0; j < 1; j++) + { + transparent_crc(g_405[i][j].f0, "g_405[i][j].f0", print_hash_value); + transparent_crc(g_405[i][j].f1, "g_405[i][j].f1", print_hash_value); + if (print_hash_value) printf("index = [%d][%d]\n", i, j); + + } + } + transparent_crc(g_471, "g_471", print_hash_value); + transparent_crc(g_555, "g_555", print_hash_value); + for (i = 0; i < 9; i++) + { + transparent_crc(g_556[i], "g_556[i]", print_hash_value); + if (print_hash_value) printf("index = [%d]\n", i); + + } + platform_main_end(crc32_context ^ 0xFFFFFFFFUL, print_hash_value); + return 0; +} + +/************************ statistics ************************* +XXX max struct depth: 0 +breakdown: + depth: 0, occurrence: 231 +XXX total union variables: 28 + +XXX non-zero bitfields defined in structs: 0 +XXX zero bitfields defined in structs: 0 +XXX const bitfields defined in structs: 0 +XXX volatile bitfields defined in structs: 0 +XXX structs with bitfields in the program: 0 +breakdown: +XXX full-bitfields structs in the program: 0 +breakdown: +XXX times a bitfields struct's address is taken: 0 +XXX times a bitfields struct on LHS: 0 +XXX times a bitfields struct on RHS: 0 +XXX times a single bitfield on LHS: 0 +XXX times a single bitfield on RHS: 0 + +XXX max expression depth: 45 +breakdown: + depth: 1, occurrence: 161 + depth: 2, occurrence: 47 + depth: 3, occurrence: 4 + depth: 4, occurrence: 3 + depth: 5, occurrence: 2 + depth: 6, occurrence: 1 + depth: 7, occurrence: 2 + depth: 10, occurrence: 2 + depth: 11, occurrence: 1 + depth: 12, occurrence: 1 + depth: 14, occurrence: 2 + depth: 15, occurrence: 1 + depth: 16, occurrence: 2 + depth: 17, occurrence: 1 + depth: 18, occurrence: 1 + depth: 19, occurrence: 1 + depth: 20, occurrence: 1 + depth: 25, occurrence: 1 + depth: 27, occurrence: 1 + depth: 28, occurrence: 1 + depth: 30, occurrence: 1 + depth: 34, occurrence: 1 + depth: 45, occurrence: 1 + +XXX total number of pointers: 222 + +XXX times a variable address is taken: 413 +XXX times a pointer is dereferenced on RHS: 116 +breakdown: + depth: 1, occurrence: 105 + depth: 2, occurrence: 10 + depth: 3, occurrence: 1 +XXX times a pointer is dereferenced on LHS: 91 +breakdown: + depth: 1, occurrence: 89 + depth: 2, occurrence: 1 + depth: 3, occurrence: 1 +XXX times a pointer is compared with null: 9 +XXX times a pointer is compared with address of another variable: 1 +XXX times a pointer is compared with another pointer: 3 +XXX times a pointer is qualified to be dereferenced: 2461 + +XXX max dereference level: 3 +breakdown: + level: 0, occurrence: 0 + level: 1, occurrence: 541 + level: 2, occurrence: 44 + level: 3, occurrence: 6 +XXX number of pointers point to pointers: 37 +XXX number of pointers point to scalars: 174 +XXX number of pointers point to structs: 0 +XXX percent of pointers has null in alias set: 30.2 +XXX average alias set size: 1.65 + +XXX times a non-volatile is read: 686 +XXX times a non-volatile is write: 318 +XXX times a volatile is read: 0 +XXX times read thru a pointer: 0 +XXX times a volatile is write: 0 +XXX times written thru a pointer: 0 +XXX times a volatile is available for access: 0 +XXX percentage of non-volatile access: 100 + +XXX forward jumps: 1 +XXX backward jumps: 2 + +XXX stmts: 167 +XXX max block depth: 5 +breakdown: + depth: 0, occurrence: 32 + depth: 1, occurrence: 19 + depth: 2, occurrence: 34 + depth: 3, occurrence: 34 + depth: 4, occurrence: 23 + depth: 5, occurrence: 25 + +XXX percentage a fresh-made variable is used: 17.7 +XXX percentage an existing variable is used: 82.3 +********************* end of statistics **********************/ + diff --git a/tests/fuzz/3.c.txt b/tests/fuzz/3.c.txt new file mode 100644 index 00000000..ab401bb1 --- /dev/null +++ b/tests/fuzz/3.c.txt @@ -0,0 +1 @@ +checksum = EE4B2FFC diff --git a/tests/fuzz/4.c b/tests/fuzz/4.c new file mode 100644 index 00000000..01949b54 --- /dev/null +++ b/tests/fuzz/4.c @@ -0,0 +1,216 @@ +/* + * This is a RANDOMLY GENERATED PROGRAM. + * + * Generator: csmith 2.2.0 + * Git version: a8697aa + * Options: --no-volatiles --no-math64 --no-packed-struct --max-block-depth 2 --max-block-size 2 --max-expr-complexity 2 --max-funcs 2 + * Seed: 3993484092 + */ + +#include "csmith.h" + + +static long __undefined; + +/* --- Struct/Union Declarations --- */ +struct S0 { + int16_t f0; + const int8_t f1; + int32_t f2; + const uint8_t f3; + unsigned f4 : 27; + uint32_t f5; + const int32_t f6; + int8_t f7; + uint32_t f8; +}; + +/* --- GLOBAL VARIABLES --- */ +static int8_t g_5[8] = {0x8EL,0x8EL,0x8EL,0x8EL,0x8EL,0x8EL,0x8EL,0x8EL}; +static int8_t *g_4 = &g_5[7]; +static int32_t g_6[4][10] = {{0x158018D9L,1L,0L,0xA7EF5D87L,0xA7EF5D87L,0L,1L,0x158018D9L,0x158018D9L,1L},{0x158018D9L,0xA7EF5D87L,1L,1L,0xA7EF5D87L,0x158018D9L,0L,0L,0x158018D9L,0xA7EF5D87L},{0xA7EF5D87L,1L,1L,0xA7EF5D87L,0x158018D9L,0L,0L,0x158018D9L,0xA7EF5D87L,1L},{0xA7EF5D87L,0xA7EF5D87L,0L,1L,0x158018D9L,0x158018D9L,1L,0L,0xA7EF5D87L,0xA7EF5D87L}}; +static int32_t g_11 = 0L; +static int32_t g_14[2][4] = {{0x69FF4CAAL,0x69FF4CAAL,0x69FF4CAAL,0x69FF4CAAL},{0x69FF4CAAL,0x69FF4CAAL,0x69FF4CAAL,0x69FF4CAAL}}; +static int32_t *g_13 = &g_14[0][0]; +static int32_t g_21 = 8L; + + +/* --- FORWARD DECLARATIONS --- */ +static uint8_t func_1(void); +static int32_t func_2(int8_t * p_3); + + +/* --- FUNCTIONS --- */ +/* ------------------------------------------ */ +/* + * reads : g_4 g_6 g_11 g_21 g_14 g_5 + * writes: g_6 g_11 g_13 g_14 g_21 g_5 + */ +static uint8_t func_1(void) +{ /* block id: 0 */ + int32_t *l_18 = &g_14[0][0]; + (*l_18) = func_2(g_4); + for (g_11 = 24; (g_11 > 9); g_11 = safe_sub_func_int8_t_s_s(g_11, 8)) + { /* block id: 17 */ + int32_t *l_24 = &g_21; + for (g_21 = (-6); (g_21 >= 8); g_21 = safe_add_func_int16_t_s_s(g_21, 9)) + { /* block id: 20 */ + (*l_18) ^= ((void*)0 == l_24); + (*l_18) = ((*l_24) ^ 1L); + } + for (g_21 = 5; (g_21 >= 0); g_21 -= 1) + { /* block id: 26 */ + const struct S0 l_29[7][5] = {{{0xB1C2L,0x10L,0xA4AECA2AL,0x28L,485,4294967294UL,-1L,0x00L,0x971708C9L},{-5L,0xA5L,0x7FB6F8A3L,0UL,6341,1UL,0xA1BBDB81L,-1L,4294967286UL},{2L,7L,0x396434D1L,0xEAL,9469,0xEFE6B3E9L,0x2DA72DF2L,0x08L,9UL},{0x32E7L,-1L,-4L,5UL,7688,4294967292UL,0L,7L,7UL},{-5L,0xA5L,0x7FB6F8A3L,0UL,6341,1UL,0xA1BBDB81L,-1L,4294967286UL}},{{0xC6C8L,0xCDL,-8L,0xD2L,1855,0x1A504A86L,0x6A6C3430L,0L,0x372A7181L},{-2L,0x94L,-1L,255UL,5118,0UL,3L,0xBDL,2UL},{-10L,0x93L,1L,1UL,3983,4294967288UL,0xC0DB1B79L,7L,4294967293UL},{0x254FL,0xD2L,1L,1UL,8061,0xDF93C581L,0xDA77236CL,0x42L,1UL},{-2L,0x94L,-1L,255UL,5118,0UL,3L,0xBDL,2UL}},{{3L,0xC4L,0x293E9FC6L,255UL,4667,2UL,0x4CA9A92EL,8L,6UL},{0xB1C2L,0x10L,0xA4AECA2AL,0x28L,485,4294967294UL,-1L,0x00L,0x971708C9L},{2L,7L,0x396434D1L,0xEAL,9469,0xEFE6B3E9L,0x2DA72DF2L,0x08L,9UL},{-5L,0xA5L,0x7FB6F8A3L,0UL,6341,1UL,0xA1BBDB81L,-1L,4294967286UL},{-5L,0xA5L,0x7FB6F8A3L,0UL,6341,1UL,0xA1BBDB81L,-1L,4294967286UL}},{{-10L,0x93L,1L,1UL,3983,4294967288UL,0xC0DB1B79L,7L,4294967293UL},{0xC6C8L,0xCDL,-8L,0xD2L,1855,0x1A504A86L,0x6A6C3430L,0L,0x372A7181L},{0xAF73L,7L,0x2A224D52L,0x10L,5709,0UL,0x3A9A7891L,-1L,0x7C6C5FB3L},{-2L,0x94L,-1L,255UL,5118,0UL,3L,0xBDL,2UL},{-2L,0x94L,-1L,255UL,5118,0UL,3L,0xBDL,2UL}},{{2L,7L,0x396434D1L,0xEAL,9469,0xEFE6B3E9L,0x2DA72DF2L,0x08L,9UL},{3L,0xC4L,0x293E9FC6L,255UL,4667,2UL,0x4CA9A92EL,8L,6UL},{0x32E7L,-1L,-4L,5UL,7688,4294967292UL,0L,7L,7UL},{0xB1C2L,0x10L,0xA4AECA2AL,0x28L,485,4294967294UL,-1L,0x00L,0x971708C9L},{0xB1C2L,0x10L,0xA4AECA2AL,0x28L,485,4294967294UL,-1L,0x00L,0x971708C9L}},{{0xAF73L,7L,0x2A224D52L,0x10L,5709,0UL,0x3A9A7891L,-1L,0x7C6C5FB3L},{0xC6C8L,0xCDL,-8L,0xD2L,1855,0x1A504A86L,0x6A6C3430L,0L,0x372A7181L},{0x254FL,0xD2L,1L,1UL,8061,0xDF93C581L,0xDA77236CL,0x42L,1UL},{0xC6C8L,0xCDL,-8L,0xD2L,1855,0x1A504A86L,0x6A6C3430L,0L,0x372A7181L},{0xC6C8L,0xCDL,-8L,0xD2L,1855,0x1A504A86L,0x6A6C3430L,0L,0x372A7181L}},{{0x32E7L,-1L,-4L,5UL,7688,4294967292UL,0L,7L,7UL},{3L,0xC4L,0x293E9FC6L,255UL,4667,2UL,0x4CA9A92EL,8L,6UL},{-5L,0xA5L,0x7FB6F8A3L,0UL,6341,1UL,0xA1BBDB81L,-1L,4294967286UL},{0xB1C2L,0x10L,0xA4AECA2AL,0x28L,485,4294967294UL,-1L,0x00L,0x971708C9L},{3L,0xC4L,0x293E9FC6L,255UL,4667,2UL,0x4CA9A92EL,8L,6UL}}}; + int i, j; + (*l_18) = (safe_mod_func_uint8_t_u_u((((safe_mul_func_int8_t_s_s((g_5[g_21] = (*g_4)), 0xFDL)) | g_14[1][2]) <= (*l_18)), 254UL)); + (*l_18) = (((l_29[4][2] , (void*)0) != &g_14[0][0]) <= l_29[4][2].f1); + } + } + return (*l_18); +} + + +/* ------------------------------------------ */ +/* + * reads : g_6 g_11 + * writes: g_6 g_11 g_13 + */ +static int32_t func_2(int8_t * p_3) +{ /* block id: 1 */ + uint32_t l_15[4][3][4] = {{{0x1943EC3EL,8UL,1UL,0x58E7EA1BL},{0x1943EC3EL,2UL,0x58E7EA1BL,4294967292UL},{0UL,1UL,0UL,4294967293UL}},{{0x4898D08FL,1UL,0x4898D08FL,4294967292UL},{0UL,0x1943EC3EL,4294967292UL,0x4898D08FL},{2UL,4294967293UL,4294967292UL,0UL}},{{0UL,0x1943EC3EL,8UL,0x1943EC3EL},{4294967292UL,0x58E7EA1BL,0x1943EC3EL,0x1943EC3EL},{0x1943EC3EL,0x58E7EA1BL,0x4898D08FL,2UL}},{{1UL,0UL,8UL,8UL},{0x4898D08FL,1UL,8UL,0UL},{0x58E7EA1BL,4294967292UL,0x58E7EA1BL,0x1943EC3EL}}}; + int i, j, k; + for (g_6[2][7] = (-24); (g_6[2][7] >= (-7)); g_6[2][7] = safe_add_func_uint32_t_u_u(g_6[2][7], 1)) + { /* block id: 4 */ + int32_t *l_9 = (void*)0; + int32_t *l_10 = &g_11; + (*l_10) &= 0x1B35D569L; + for (g_11 = 0; (g_11 <= 7); g_11 += 1) + { /* block id: 8 */ + int32_t **l_12[6] = {&l_10,&l_10,&l_10,&l_10,&l_10,&l_10}; + int i; + g_13 = l_9; + l_15[1][2][0]--; + } + } + return l_15[0][1][1]; +} + + + + +/* ---------------------------------------- */ +int main (int argc, char* argv[]) +{ + int i, j; + int print_hash_value = 0; + if (argc == 2 && strcmp(argv[1], "1") == 0) print_hash_value = 1; + platform_main_begin(); + crc32_gentab(); + func_1(); + for (i = 0; i < 8; i++) + { + transparent_crc(g_5[i], "g_5[i]", print_hash_value); + if (print_hash_value) printf("index = [%d]\n", i); + + } + for (i = 0; i < 4; i++) + { + for (j = 0; j < 10; j++) + { + transparent_crc(g_6[i][j], "g_6[i][j]", print_hash_value); + if (print_hash_value) printf("index = [%d][%d]\n", i, j); + + } + } + transparent_crc(g_11, "g_11", print_hash_value); + for (i = 0; i < 2; i++) + { + for (j = 0; j < 4; j++) + { + transparent_crc(g_14[i][j], "g_14[i][j]", print_hash_value); + if (print_hash_value) printf("index = [%d][%d]\n", i, j); + + } + } + transparent_crc(g_21, "g_21", print_hash_value); + platform_main_end(crc32_context ^ 0xFFFFFFFFUL, print_hash_value); + return 0; +} + +/************************ statistics ************************* +XXX max struct depth: 1 +breakdown: + depth: 0, occurrence: 4 + depth: 1, occurrence: 1 +XXX total union variables: 0 + +XXX non-zero bitfields defined in structs: 1 +XXX zero bitfields defined in structs: 0 +XXX const bitfields defined in structs: 0 +XXX volatile bitfields defined in structs: 0 +XXX structs with bitfields in the program: 1 +breakdown: + indirect level: 0, occurrence: 1 +XXX full-bitfields structs in the program: 0 +breakdown: +XXX times a bitfields struct's address is taken: 0 +XXX times a bitfields struct on LHS: 0 +XXX times a bitfields struct on RHS: 1 +XXX times a single bitfield on LHS: 0 +XXX times a single bitfield on RHS: 0 + +XXX max expression depth: 6 +breakdown: + depth: 1, occurrence: 13 + depth: 2, occurrence: 8 + depth: 4, occurrence: 1 + depth: 6, occurrence: 1 + +XXX total number of pointers: 8 + +XXX times a variable address is taken: 8 +XXX times a pointer is dereferenced on RHS: 4 +breakdown: + depth: 1, occurrence: 4 +XXX times a pointer is dereferenced on LHS: 6 +breakdown: + depth: 1, occurrence: 6 +XXX times a pointer is compared with null: 1 +XXX times a pointer is compared with address of another variable: 0 +XXX times a pointer is compared with another pointer: 0 +XXX times a pointer is qualified to be dereferenced: 39 + +XXX max dereference level: 1 +breakdown: + level: 0, occurrence: 0 + level: 1, occurrence: 16 +XXX number of pointers point to pointers: 1 +XXX number of pointers point to scalars: 7 +XXX number of pointers point to structs: 0 +XXX percent of pointers has null in alias set: 25 +XXX average alias set size: 1.12 + +XXX times a non-volatile is read: 20 +XXX times a non-volatile is write: 20 +XXX times a volatile is read: 0 +XXX times read thru a pointer: 0 +XXX times a volatile is write: 0 +XXX times written thru a pointer: 0 +XXX times a volatile is available for access: 0 +XXX percentage of non-volatile access: 100 + +XXX forward jumps: 0 +XXX backward jumps: 0 + +XXX stmts: 15 +XXX max block depth: 2 +breakdown: + depth: 0, occurrence: 5 + depth: 1, occurrence: 4 + depth: 2, occurrence: 6 + +XXX percentage a fresh-made variable is used: 23.8 +XXX percentage an existing variable is used: 76.2 +FYI: the random generator makes assumptions about the integer size. See platform.info for more details. +********************* end of statistics **********************/ + diff --git a/tests/fuzz/4.c.txt b/tests/fuzz/4.c.txt new file mode 100644 index 00000000..f27bcce5 --- /dev/null +++ b/tests/fuzz/4.c.txt @@ -0,0 +1 @@ +checksum = 157CE2A8 diff --git a/tests/fuzz/5.c b/tests/fuzz/5.c new file mode 100644 index 00000000..a62c18a3 --- /dev/null +++ b/tests/fuzz/5.c @@ -0,0 +1,13 @@ +#include <stdio.h> +#include <string.h> + +int main(int argc, char **argv) { + printf("argc %d\n", argc); + char buffer[100]; + for (int i = 0; i < 100; i++) buffer[i] = argc*(argc > 10 ? (argc*i) % 3 : (i*i)); + memset(&buffer[10], -114, argc+25); + for(int i = 0; i < 100; i++) printf("%d:%d\n", i, buffer[i]); + //memset 5243040,-114,6,false,6 + return buffer[15]; +} + diff --git a/tests/fuzz/5.c.txt b/tests/fuzz/5.c.txt new file mode 100644 index 00000000..0c9d6dcd --- /dev/null +++ b/tests/fuzz/5.c.txt @@ -0,0 +1,101 @@ +argc 1 +0:0 +1:1 +2:4 +3:9 +4:16 +5:25 +6:36 +7:49 +8:64 +9:81 +10:-114 +11:-114 +12:-114 +13:-114 +14:-114 +15:-114 +16:-114 +17:-114 +18:-114 +19:-114 +20:-114 +21:-114 +22:-114 +23:-114 +24:-114 +25:-114 +26:-114 +27:-114 +28:-114 +29:-114 +30:-114 +31:-114 +32:-114 +33:-114 +34:-114 +35:-114 +36:16 +37:89 +38:-92 +39:-15 +40:64 +41:-111 +42:-28 +43:57 +44:-112 +45:-23 +46:68 +47:-95 +48:0 +49:97 +50:-60 +51:41 +52:-112 +53:-7 +54:100 +55:-47 +56:64 +57:-79 +58:36 +59:-103 +60:16 +61:-119 +62:4 +63:-127 +64:0 +65:-127 +66:4 +67:-119 +68:16 +69:-103 +70:36 +71:-79 +72:64 +73:-47 +74:100 +75:-7 +76:-112 +77:41 +78:-60 +79:97 +80:0 +81:-95 +82:68 +83:-23 +84:-112 +85:57 +86:-28 +87:-111 +88:64 +89:-15 +90:-92 +91:89 +92:16 +93:-55 +94:-124 +95:65 +96:0 +97:-63 +98:-124 +99:73 diff --git a/tests/fuzz/7.c b/tests/fuzz/7.c new file mode 100644 index 00000000..45c0096d --- /dev/null +++ b/tests/fuzz/7.c @@ -0,0 +1,852 @@ +/* + * This is a RANDOMLY GENERATED PROGRAM. + * + * Generator: csmith 2.2.0 + * Git version: a8697aa + * Options: --no-volatiles --no-math64 --no-packed-struct + * Seed: 4255021480 + */ + +#include "csmith.h" + + +static long __undefined; + +/* --- Struct/Union Declarations --- */ +union U0 { + uint32_t f0; + uint32_t f1; + uint16_t f2; + int32_t f3; + int16_t f4; +}; + +union U1 { + int32_t f0; + int8_t f1; +}; + +union U2 { + signed f0 : 31; + uint8_t f1; +}; + +/* --- GLOBAL VARIABLES --- */ +static union U2 g_9[5] = {{5L},{5L},{5L},{5L},{5L}}; +static int32_t g_11 = 0xE5C285CEL; +static const int32_t *g_16 = &g_11; +static uint8_t g_66[1] = {0xC8L}; +static uint8_t g_71 = 255UL; +static int32_t g_75 = 0xD78BEA8EL; +static int8_t g_76[5] = {0x1AL,0x1AL,0x1AL,0x1AL,0x1AL}; +static int16_t g_77 = 0x065BL; +static uint32_t g_78[7][9] = {{0x1A9F1398L,0xB1F15F1DL,0x4BD9F5B6L,0x1A9F1398L,0x8559CE79L,0xA768FB0CL,0xB1AAE879L,4294967293UL,0x8559CE79L},{0x8327BC4AL,0xF31BC463L,8UL,7UL,1UL,7UL,1UL,0x00823388L,1UL},{0x9C36DE1FL,0x19045039L,0xA768FB0CL,0x9C36DE1FL,0xB1F15F1DL,4294967293UL,4294967293UL,0x7078C3FCL,0x8559CE79L},{0x6E6AF575L,0x6E6AF575L,1UL,0x8327BC4AL,7UL,1UL,0x00823388L,0x00823388L,1UL},{0x8559CE79L,0x19045039L,0x9C36DE1FL,0x1A9F1398L,4294967293UL,0x9C36DE1FL,4294967291UL,4294967293UL,0xB1F15F1DL},{0x6E6AF575L,1UL,0x8327BC4AL,0x24791A13L,0x00823388L,1UL,0x24791A13L,0x6E6AF575L,7UL},{0x19045039L,0xA768FB0CL,0x19045039L,0x7078C3FCL,0x7078C3FCL,0xB1AAE879L,0x8559CE79L,0x7078C3FCL,0xA768FB0CL}}; +static int32_t *g_83[8][10] = {{&g_11,&g_11,&g_11,&g_11,&g_11,&g_11,&g_11,&g_11,&g_11,&g_11},{&g_11,&g_11,&g_11,&g_11,&g_11,&g_11,&g_11,&g_11,&g_11,&g_11},{&g_11,&g_11,&g_11,&g_11,&g_11,&g_11,&g_11,&g_11,&g_11,&g_11},{&g_11,&g_11,&g_11,&g_11,&g_11,&g_11,&g_11,&g_11,&g_11,&g_11},{&g_11,&g_11,&g_11,&g_11,&g_11,&g_11,&g_11,&g_11,&g_11,&g_11},{&g_11,&g_11,&g_11,&g_11,&g_11,&g_11,&g_11,&g_11,&g_11,&g_11},{&g_11,&g_11,&g_11,&g_11,&g_11,&g_11,&g_11,&g_11,&g_11,&g_11},{&g_11,&g_11,&g_11,&g_11,&g_11,&g_11,&g_11,&g_11,&g_11,&g_11}}; +static int32_t **g_82 = &g_83[0][9]; +static int32_t g_102 = 0xFD02F95CL; +static uint16_t g_135 = 65534UL; +static int32_t g_144 = 0x2D1F4F54L; +static const uint16_t g_160 = 1UL; +static union U0 g_181 = {4294967292UL}; +static union U0 *g_183 = &g_181; +static union U1 g_202 = {0xD13A1308L}; +static union U1 *g_201 = &g_202; +static int16_t g_209 = 2L; +static int32_t g_211 = 0xAD8558D5L; +static uint16_t g_231 = 65527UL; +static union U1 *g_261 = &g_202; +static union U2 *g_282 = (void*)0; +static union U2 **g_281 = &g_282; +static const int16_t g_328 = 2L; +static int8_t g_390 = 0x65L; +static int32_t g_468 = 0x425A7515L; +static int32_t **g_537 = &g_83[6][2]; +static uint32_t g_547 = 0UL; +static const uint32_t g_573 = 4294967288UL; +static const uint32_t g_575 = 4294967291UL; +static const uint32_t *g_574 = &g_575; +static int16_t g_582 = 0x5594L; +static union U0 **g_587 = &g_183; +static union U0 ***g_586 = &g_587; +static uint16_t *g_593 = &g_231; +static uint16_t **g_592 = &g_593; +static const union U1 g_649 = {-1L}; +static const union U1 *g_648 = &g_649; +static int16_t g_666 = 0xB689L; +static uint32_t g_668 = 2UL; +static union U2 ***g_691 = &g_281; +static union U2 *** const *g_690 = &g_691; +static int32_t g_756 = 0x25866B22L; +static uint8_t g_860 = 0x3DL; +static uint16_t g_894 = 0xD1D4L; +static const int32_t g_947 = 2L; +static uint16_t g_966[5][3] = {{1UL,1UL,0x3378L},{1UL,1UL,1UL},{65535UL,0x3378L,1UL},{1UL,1UL,0x3378L},{1UL,0x3378L,65535UL}}; +static int8_t *g_1030 = (void*)0; +static int8_t **g_1029 = &g_1030; +static union U1 g_1059[9][9] = {{{2L},{9L},{1L},{9L},{2L},{2L},{2L},{1L},{1L}},{{5L},{0x9B1D2FFCL},{0x256D147CL},{0x9B1D2FFCL},{5L},{0x9B1D2FFCL},{5L},{0x256D147CL},{0x256D147CL}},{{9L},{2L},{2L},{2L},{9L},{1L},{9L},{2L},{9L}},{{0x9B1D2FFCL},{5L},{5L},{5L},{0x9B1D2FFCL},{0x256D147CL},{0x256D147CL},{5L},{0x9B1D2FFCL}},{{1L},{9L},{1L},{9L},{2L},{2L},{2L},{9L},{1L}},{{5L},{0x9B1D2FFCL},{0x256D147CL},{0x9B1D2FFCL},{5L},{5L},{5L},{0x256D147CL},{0x256D147CL}},{{9L},{1L},{2L},{2L},{9L},{1L},{9L},{2L},{2L}},{{0x9B1D2FFCL},{5L},{5L},{5L},{0x9B1D2FFCL},{0x256D147CL},{0x9B1D2FFCL},{5L},{0x9B1D2FFCL}},{{1L},{9L},{9L},{9L},{2L},{2L},{2L},{9L},{1L}}}; +static int32_t g_1124 = 0L; +static int16_t g_1254 = 5L; +static int32_t g_1294 = 0x5FC158E7L; +static uint32_t *g_1326 = &g_78[4][2]; +static int16_t *g_1346[7] = {&g_209,&g_582,&g_209,&g_582,&g_1254,&g_1254,&g_1254}; +static union U0 *g_1354 = &g_181; +static uint16_t g_1418 = 0x7548L; +static int8_t * const **g_1432 = (void*)0; +static union U1 **g_1483[6] = {&g_261,&g_261,&g_261,&g_261,&g_261,&g_261}; +static int8_t g_1501 = (-1L); +static const int8_t *g_1582[8] = {(void*)0,(void*)0,(void*)0,&g_1059[4][2].f1,(void*)0,(void*)0,(void*)0,(void*)0}; +static const int8_t **g_1581 = &g_1582[6]; +static const int8_t ***g_1580 = &g_1581; +static uint8_t g_1597 = 1UL; +static int32_t g_1613 = 0L; +static uint8_t **g_1628 = (void*)0; +static int32_t ** const *g_1630 = &g_82; +static int32_t ** const **g_1629 = &g_1630; +static int16_t g_1665[1] = {(-8L)}; + + +/* --- FORWARD DECLARATIONS --- */ +static int16_t func_1(void); +static const int32_t * func_2(uint8_t p_3, union U2 p_4, int32_t * p_5, union U0 p_6, const union U1 p_7); +static int32_t func_19(int32_t p_20); +static const union U1 func_23(const int32_t * p_24, int8_t p_25, uint8_t p_26, const union U0 p_27, const int32_t * p_28); +static const int32_t * func_29(int32_t ** p_30, int32_t * p_31, int32_t * p_32, uint32_t p_33); +static int32_t * func_36(int32_t ** p_37); +static int32_t ** func_38(int16_t p_39, int8_t p_40, union U2 p_41, union U0 p_42); +static int8_t func_45(uint8_t p_46, uint16_t p_47, union U2 p_48, int32_t ** p_49, const uint32_t p_50); +static int8_t func_55(uint8_t p_56, union U1 p_57, int32_t * p_58, uint8_t p_59); +static int32_t * func_61(int16_t p_62, const int32_t ** p_63); + + +/* --- FUNCTIONS --- */ +/* ------------------------------------------ */ +/* + * reads : g_9 g_9.f0 g_11 g_16 g_66 g_71 g_78 g_82 g_77 g_181.f4 g_209 g_202.f0 g_181.f2 g_144 g_83 g_201 g_202 g_537 g_102 g_76 g_231 g_582 g_586 g_575 g_592 g_593 g_468 g_202.f1 g_261 g_649.f0 g_183 g_181 g_574 g_547 g_666 g_668 g_894 g_1124 g_587 g_181.f0 g_1630 g_281 g_282 g_690 g_691 + * writes: g_16 g_66 g_71 g_78 g_76 g_181.f4 g_144 g_547 g_390 g_135 g_83 g_75 g_102 g_11 g_574 g_586 g_231 g_468 g_77 g_209 g_648 g_1124 g_181.f0 g_183 g_282 g_202 + */ +static int16_t func_1(void) +{ /* block id: 0 */ + int8_t l_8 = 0xD2L; + int32_t *l_10 = &g_11; + union U0 l_12[3] = {{0xC63C4078L},{0xC63C4078L},{0xC63C4078L}}; + const union U1 l_13 = {0x54DBBE56L}; + const int32_t *l_15 = &l_12[2].f3; + const int32_t **l_14[1]; + union U2 **l_1476[1]; + union U1 ** const l_1484 = &g_261; + int16_t l_1489 = (-3L); + int32_t l_1490[4][9][7] = {{{0x7722092EL,0xE7360E5FL,0x6D151BE1L,6L,(-5L),8L,0x348F7672L},{0x09844A68L,(-1L),(-9L),1L,0xFFB9EB74L,0xB7C332CFL,0x6178C1E9L},{0L,0x4AEF49C4L,0x579BFEBAL,0x1115582EL,0x1724644DL,1L,0x9A5CF446L},{0xB336F930L,1L,1L,0L,0x3AF81339L,9L,0xB12F7DAAL},{0x61FAFD88L,6L,9L,0xA08F10A1L,0x06B6F60EL,0xFFB9EB74L,0xEAFC93F5L},{6L,0xA08F10A1L,0x06B6F60EL,(-8L),(-7L),0xB12F7DAAL,0xB4CB5279L},{(-5L),(-2L),0xBA853D88L,(-8L),0L,0xDE8A14BCL,0xE7360E5FL},{0xF7A455E7L,0x579BFEBAL,0xEAFC93F5L,0xA08F10A1L,0x1DE7A7EEL,0x348F7672L,(-1L)},{0x579BFEBAL,(-2L),0xDE8A14BCL,0L,0xCC05669DL,0xCC05669DL,0x1E6C6902L}},{{0x3AF81339L,(-7L),(-1L),0x1115582EL,(-2L),0xCB6A97FAL,(-5L)},{3L,(-1L),(-2L),1L,0xEAFC93F5L,0x781D787BL,0x85A2F7F1L},{0x1E6C6902L,0x781D787BL,(-9L),6L,1L,(-7L),1L},{0x579BFEBAL,8L,0x09844A68L,0xE7360E5FL,0x6178C1E9L,0x3AF81339L,1L},{0x348F7672L,(-1L),(-9L),0x566121C2L,4L,0x06B6F60EL,0xCC05669DL},{1L,0x9A5CF446L,(-2L),0L,0x1115582EL,0x1724644DL,0x1115582EL},{0x09844A68L,0x579BFEBAL,(-1L),0x1724644DL,0xC388F8C3L,0xF7A455E7L,0L},{9L,0xA34E9BADL,0xDE8A14BCL,0L,0xF7A455E7L,0x29ABAF8CL,(-1L)},{(-1L),1L,0xEAFC93F5L,1L,1L,2L,9L}},{{(-1L),0x1DE7A7EEL,3L,0x7BF25D80L,(-7L),9L,0x348F7672L},{0xEAFC93F5L,0x1DE7A7EEL,0x7722092EL,0xA34E9BADL,8L,9L,2L},{(-1L),1L,0x0CEE732EL,6L,2L,0x9E61483AL,0x9E61483AL},{0xBA853D88L,0xA34E9BADL,0x6EC18B86L,0xCE872C2BL,0xA34E9BADL,(-1L),0x3AF81339L},{(-7L),0x579BFEBAL,0L,5L,0xEB035C34L,0x6EC18B86L,0x7722092EL},{0x6D151BE1L,0xBBCB4E5FL,0xA34E9BADL,(-1L),(-1L),0xEAFC93F5L,9L},{1L,0x9E61483AL,0xB336F930L,0xAA26BCE5L,0x566121C2L,0x9E61483AL,0xEB035C34L},{2L,0x85A2F7F1L,0xEAC2EEC8L,0xEAFC93F5L,0xBBCB4E5FL,0xB4CB5279L,0x1E6C6902L},{1L,0xFFB9EB74L,0x7722092EL,(-1L),0x96B34700L,0x1FBA7239L,0xB336F930L}},{{0x6D151BE1L,0xC388F8C3L,0x1579862BL,0x06B6F60EL,0x1E6C6902L,0x06B6F60EL,2L},{(-7L),0x09844A68L,2L,0xB4CB5279L,5L,1L,(-1L)},{3L,2L,(-1L),0xEB035C34L,(-1L),0xC388F8C3L,0x85A2F7F1L},{0x4EC6A0E7L,0L,1L,0x3AF81339L,0x7BF25D80L,(-1L),0L},{(-1L),(-1L),0x3AF81339L,(-8L),4L,0x61FAFD88L,0L},{4L,0x929F3853L,0xCB6A97FAL,0L,0xAA26BCE5L,0xCE872C2BL,0x85A2F7F1L},{0x566121C2L,0x4AEF49C4L,6L,0x4EC6A0E7L,(-1L),(-1L),(-1L)},{0x61FAFD88L,0x29ABAF8CL,0xE7360E5FL,0xE7360E5FL,0L,(-2L),2L},{0xB4CB5279L,(-2L),0x1DE7A7EEL,0xE7360E5FL,0xDE8A14BCL,0L,(-8L)}}}; + int32_t l_1499 = 0L; + int32_t l_1503 = 1L; + uint32_t l_1505[3][5][2] = {{{4294967289UL,1UL},{0UL,1UL},{4294967289UL,1UL},{0UL,1UL},{4294967289UL,1UL}},{{0UL,1UL},{4294967289UL,1UL},{0UL,1UL},{4294967289UL,1UL},{0UL,1UL}},{{4294967289UL,1UL},{0UL,1UL},{4294967289UL,1UL},{0UL,1UL},{4294967289UL,1UL}}}; + int32_t l_1530 = (-3L); + int32_t l_1550 = 5L; + int32_t l_1557 = 5L; + union U1 *l_1563 = &g_1059[6][4]; + uint32_t l_1622 = 0x24618042L; + uint16_t l_1656 = 1UL; + uint8_t *l_1682[9][4] = {{&g_66[0],&g_66[0],&g_66[0],&g_66[0]},{&g_66[0],&g_66[0],&g_66[0],&g_66[0]},{&g_66[0],&g_66[0],&g_66[0],&g_66[0]},{&g_66[0],&g_66[0],&g_66[0],&g_66[0]},{&g_66[0],&g_66[0],&g_66[0],&g_66[0]},{&g_66[0],&g_66[0],&g_66[0],&g_66[0]},{&g_66[0],&g_66[0],&g_66[0],&g_66[0]},{&g_66[0],&g_66[0],&g_66[0],&g_66[0]},{&g_66[0],&g_66[0],&g_66[0],&g_66[0]}}; + uint8_t **l_1681 = &l_1682[8][2]; + int32_t l_1684 = 0L; + int i, j, k; + for (i = 0; i < 1; i++) + l_14[i] = &l_15; + for (i = 0; i < 1; i++) + l_1476[i] = (void*)0; + g_16 = func_2(l_8, g_9[0], l_10, l_12[2], l_13); + for (l_8 = 0; (l_8 < 3); l_8 = safe_add_func_int16_t_s_s(l_8, 8)) + { /* block id: 6 */ + int32_t l_1456 = 0L; + int32_t l_1493 = (-5L); + int32_t l_1494 = 0xCC45616AL; + int32_t l_1498 = 0xF676537EL; + int32_t l_1502[5]; + int8_t l_1525 = (-1L); + const int8_t l_1562 = 8L; + const uint32_t **l_1594 = &g_574; + union U1 l_1610 = {1L}; + union U2 l_1654 = {2L}; + uint32_t l_1685 = 0x72C9CEDCL; + int i; + for (i = 0; i < 5; i++) + l_1502[i] = (-1L); + if (func_19(g_9[0].f0)) + { /* block id: 926 */ + int8_t l_1454 = 0xD7L; + int32_t l_1457[6][4] = {{0x7FED4B30L,1L,0x7FED4B30L,1L},{0x7FED4B30L,1L,0x7FED4B30L,1L},{0x7FED4B30L,1L,0x7FED4B30L,1L},{0x7FED4B30L,1L,0x7FED4B30L,1L},{0x7FED4B30L,1L,0x7FED4B30L,1L},{0x7FED4B30L,1L,0x7FED4B30L,1L}}; + const int32_t *l_1466 = &g_468; + uint16_t l_1488[6]; + union U2 ***l_1529[9] = {&l_1476[0],&l_1476[0],&l_1476[0],&l_1476[0],&l_1476[0],&l_1476[0],&l_1476[0],&l_1476[0],&l_1476[0]}; + int16_t l_1546 = 0x676EL; + const union U0 l_1565 = {4294967292UL}; + union U1 *l_1571 = &g_1059[4][2]; + uint32_t *l_1591 = &l_1505[0][0][0]; + int8_t l_1658 = 3L; + int16_t l_1659 = 0xE414L; + uint32_t l_1660 = 0x579CC0EBL; + uint32_t l_1666 = 0xE040432BL; + uint32_t l_1671 = 0xF8DE41A5L; + union U1 *l_1672 = (void*)0; + union U1 *l_1673[3]; + union U1 *l_1674 = &g_202; + int i, j; + for (i = 0; i < 6; i++) + l_1488[i] = 8UL; + for (i = 0; i < 3; i++) + l_1673[i] = (void*)0; + for (g_144 = 0; (g_144 <= 0); g_144 += 1) + { /* block id: 929 */ + int16_t *l_1455[10] = {&l_12[2].f4,&l_12[2].f4,&l_12[2].f4,&l_12[2].f4,&l_12[2].f4,&l_12[2].f4,&l_12[2].f4,&l_12[2].f4,&l_12[2].f4,&l_12[2].f4}; + uint8_t *l_1463 = &g_860; + const int32_t **l_1487 = &l_15; + int32_t l_1496 = (-2L); + int32_t l_1497 = 0xC7FAF769L; + union U2 l_1538 = {-1L}; + const int8_t l_1539[6][6][7] = {{{0L,0x75L,2L,(-6L),(-6L),0xD3L,0x75L},{0x84L,0L,0x69L,0L,0x84L,0xF4L,7L},{0L,0L,0x75L,0xD3L,(-6L),0L,0x75L},{0xC1L,0x6CL,0x92L,0xF4L,0xC1L,0xF4L,0x92L},{1L,2L,0x75L,0L,(-6L),0xD3L,0xD3L},{0x84L,0L,7L,0xF4L,7L,0x6CL,0x84L}},{{1L,1L,0x75L,2L,0L,0L,0x75L},{0L,0L,0x92L,0x6CL,0L,0L,0L},{1L,0x75L,0xD3L,0L,(-6L),0x75L,0xD3L},{0x84L,0xF4L,0x84L,0L,0x84L,0L,7L},{(-6L),1L,0x75L,0x75L,0L,0L,2L},{0x92L,0x6CL,0xC1L,0L,0xC1L,0x6CL,0L}},{{(-6L),0xD3L,0xD3L,0L,1L,0x75L,0xD3L},{0x69L,0x6CL,7L,0x6CL,0x69L,0x6CL,0x69L},{(-6L),1L,2L,0x75L,0L,(-6L),0xD3L},{0L,0L,0L,0x6CL,0x92L,0x6CL,0xC1L},{0L,0xD3L,0xD3L,(-6L),0L,0x75L,0x75L},{7L,0xF4L,0x69L,0x6CL,0x84L,0xF4L,0x69L}},{{(-6L),0L,2L,0x75L,(-6L),1L,2L},{0x92L,0xF4L,0xC1L,0xF4L,0x92L,0xF4L,0x92L},{0L,0xD3L,0x75L,1L,1L,2L,2L},{0x69L,0L,0x69L,0xF4L,7L,0xF4L,0x69L},{1L,0L,2L,2L,1L,(-6L),0xD3L},{0xC1L,0L,0x92L,0xF4L,0L,0L,0x92L}},{{0L,0x75L,2L,(-6L),0L,0xD3L,0x75L},{0x84L,0L,0x84L,0L,7L,0xF4L,7L},{1L,0L,0xD3L,0xD3L,(-6L),1L,0x75L},{0x92L,0x6CL,0x92L,0L,0xC1L,0L,0x92L},{(-6L),0x75L,0x75L,1L,(-6L),2L,0xD3L},{0x84L,0x6CL,7L,0L,7L,0x6CL,7L}},{{1L,(-6L),0x75L,2L,1L,0L,0xD3L},{0L,0x6CL,0L,0x6CL,0xC1L,0L,0xC1L},{(-6L),0x75L,2L,0L,0L,0xD3L,0xD3L},{7L,0xF4L,7L,0L,0x84L,0x6CL,7L},{0L,1L,0xD3L,0xD3L,0L,1L,2L},{0L,0xF4L,0xC1L,0x6CL,0xC1L,0xF4L,0xC1L}}}; + union U1 *l_1570 = &g_1059[4][2]; + union U0 l_1593 = {1UL}; + int i, j, k; + } + if ((*g_16)) + { /* block id: 1007 */ + union U1 *l_1599 = &g_1059[4][2]; + int32_t l_1604 = 0L; + union U2 l_1625 = {0x22466E4EL}; + uint8_t *l_1627 = (void*)0; + uint8_t **l_1626 = &l_1627; + uint32_t l_1631[4][8] = {{4294967287UL,1UL,4294967288UL,0x1CF551D9L,4294967287UL,0xFC67C7A4L,0UL,1UL},{4294967288UL,4294967288UL,0xD89884B8L,0x1CF551D9L,0xD89884B8L,4294967288UL,0xD89884B8L,4294967287UL},{0UL,0xFC67C7A4L,4294967287UL,4294967287UL,0xD89884B8L,0x8D4BD049L,1UL,4294967288UL},{0x8D4BD049L,4294967287UL,4294967287UL,1UL,0xFC67C7A4L,1UL,4294967287UL,1UL}}; + int i, j; + for (g_71 = 0; (g_71 != 40); ++g_71) + { /* block id: 1010 */ + const int32_t l_1606 = 1L; + } + } + else + { /* block id: 1031 */ + int8_t l_1655 = 0x2AL; + union U2 l_1661 = {-9L}; + int32_t *l_1662 = &l_12[2].f3; + int32_t *l_1663[9] = {&l_1493,&l_1493,&l_1493,&l_1493,&l_1493,&l_1493,&l_1493,&l_1493,&l_1493}; + int32_t l_1664 = 0L; + int i; + for (g_181.f0 = (-24); (g_181.f0 <= 30); ++g_181.f0) + { /* block id: 1034 */ + uint16_t l_1641 = 65535UL; + int8_t *l_1642[7] = {&l_1610.f1,(void*)0,&g_1059[4][2].f1,&g_1059[4][2].f1,&g_1059[4][2].f1,&l_1610.f1,&l_1610.f1}; + int32_t l_1643 = 4L; + uint8_t *l_1657 = &l_1654.f1; + int i; + (*g_587) = &l_12[0]; + g_16 = func_2(((((((void*)0 == (*g_1630)) >= (safe_mul_func_int8_t_s_s((l_1643 = l_1641), (((((safe_add_func_uint32_t_u_u((0L < ((safe_add_func_uint16_t_u_u((*g_593), 0xF41FL)) != (safe_sub_func_uint8_t_u_u(((*l_1657) = ((safe_div_func_uint16_t_u_u(((((func_45(l_1641, l_1493, (l_1654 = l_1654), &l_10, (*l_1466)) ^ l_1655) || 0x95L) >= l_1656) < (*g_593)), l_1610.f0)) == 0x3CL)), l_1658)))), l_1493)) , 6L) , (*l_15)) && l_1659) || l_1641)))) , l_1654.f1) , l_1502[1]) <= l_1660), l_1661, &l_1530, l_12[2], (*g_201)); + if (l_1641) + continue; + } + (*l_10) ^= (*l_1466); + l_1666++; + if ((*l_10)) + break; + } + for (g_547 = 0; (g_547 == 3); g_547 = safe_add_func_uint8_t_u_u(g_547, 8)) + { /* block id: 1048 */ + (***g_690) = (*g_281); + l_1493 ^= l_1494; + } + g_16 = func_2(l_1671, l_1654, &l_1503, l_12[2], ((*l_1674) = l_1610)); + } + else + { /* block id: 1054 */ + int8_t l_1683 = 0x9CL; + l_1502[1] = (safe_div_func_int32_t_s_s((safe_sub_func_int8_t_s_s(l_1562, (safe_rshift_func_uint16_t_u_u((1UL | (((void*)0 == l_1681) | (+l_1683))), 2)))), l_1684)); + if (l_1610.f1) + continue; + } + return l_1685; + } + return (*l_10); +} + + +/* ------------------------------------------ */ +/* + * reads : + * writes: + */ +static const int32_t * func_2(uint8_t p_3, union U2 p_4, int32_t * p_5, union U0 p_6, const union U1 p_7) +{ /* block id: 1 */ + return &g_11; +} + + +/* ------------------------------------------ */ +/* + * reads : g_9.f0 g_11 g_16 g_66 g_71 g_78 g_9 g_82 g_77 g_181.f4 g_209 g_202.f0 g_181.f2 g_144 g_83 g_201 g_202 g_537 g_75 g_102 g_76 g_231 g_582 g_586 g_575 g_592 g_593 g_468 g_202.f1 g_261 g_649.f0 g_183 g_181 g_574 g_666 g_668 g_894 g_1124 g_587 g_547 + * writes: g_16 g_66 g_71 g_78 g_76 g_181.f4 g_144 g_547 g_390 g_135 g_83 g_75 g_102 g_11 g_574 g_586 g_231 g_468 g_77 g_209 g_648 g_1124 + */ +static int32_t func_19(int32_t p_20) +{ /* block id: 7 */ + int32_t *l_35 = &g_11; + int32_t **l_34 = &l_35; + union U1 l_60 = {0xAD4F6C55L}; + int8_t *l_84 = &g_76[2]; + union U2 l_85 = {-1L}; + union U0 l_86 = {4294967295UL}; + int32_t *l_1453 = &g_1124; + (*l_1453) |= ((safe_mod_func_uint8_t_u_u((func_23(func_29(l_34, (p_20 , func_36(func_38(g_9[0].f0, ((*l_84) = ((safe_mul_func_int16_t_s_s((((**l_34) && func_45(p_20, (safe_mod_func_uint32_t_u_u(((safe_lshift_func_int8_t_s_s(0xC8L, (func_55(p_20, l_60, func_61((((*g_16) && 0x04518073L) > 0x36L), &g_16), g_11) , (-1L)))) , p_20), g_9[0].f0)), g_9[3], g_82, p_20)) , (**l_34)), g_77)) || (**l_34))), l_85, l_86))), &g_468, p_20), p_20, p_20, l_86, (*l_34)) , 249UL), g_894)) > p_20); + (*l_35) = p_20; + (*g_537) = ((***g_586) , (*g_82)); + return (*g_16); +} + + +/* ------------------------------------------ */ +/* + * reads : g_201 g_202 + * writes: + */ +static const union U1 func_23(const int32_t * p_24, int8_t p_25, uint8_t p_26, const union U0 p_27, const int32_t * p_28) +{ /* block id: 460 */ + uint16_t l_675 = 0UL; + uint8_t *l_684 = &g_9[0].f1; + union U2 l_685 = {0x88367F2CL}; + int32_t **l_686 = (void*)0; + int8_t *l_687[7]; + int32_t l_688 = 0xF0B46BC5L; + union U0 ****l_689 = (void*)0; + const union U2 *l_695[6] = {&l_685,&l_685,&l_685,&l_685,&l_685,&l_685}; + const union U2 **l_694 = &l_695[5]; + const union U2 ***l_693 = &l_694; + const union U2 ****l_692 = &l_693; + int32_t *l_696 = &g_11; + int32_t *l_697 = &l_688; + union U0 **l_709 = &g_183; + int8_t * const *l_716 = (void*)0; + union U0 l_718 = {4294967292UL}; + const union U1 l_725 = {0xCF474642L}; + union U2 l_762 = {-3L}; + union U0 l_770 = {4294967292UL}; + int32_t l_810 = 0x0514C231L; + int32_t l_851[10] = {0L,0L,0L,0L,0L,0L,0L,0L,0L,0L}; + int32_t l_867[5][10][3] = {{{0x9279772DL,(-1L),2L},{0x08D29043L,0xE1F44506L,0x08D29043L},{0xAAC8A26EL,0xE1B3EFFAL,(-3L)},{0xE1F44506L,1L,0x87B510FDL},{0x6D1F2783L,(-3L),0L},{0x27ECB6E8L,0xE8274EACL,0x08D29043L},{0x6D1F2783L,0xC659668AL,0xDCD37698L},{0x8C0733A2L,0x87B510FDL,6L},{0x2820589CL,0xB311FE06L,0x97A0EB8CL},{(-1L),0x4EAB465CL,0xB3893D4FL}},{{(-3L),(-1L),(-1L)},{0x411C5323L,0xBDBD4C74L,1L},{0xB311FE06L,0xDCD37698L,0xDCD37698L},{0x411C5323L,0x8C0733A2L,0xF779865EL},{0x2E256168L,2L,(-5L)},{0x63F4CD2EL,0xBDBD4C74L,0xF779865EL},{0xAAC8A26EL,1L,(-1L)},{1L,0x4EAB465CL,0x8C0733A2L},{(-1L),0xAAC8A26EL,(-1L)},{0x87B510FDL,(-2L),0xEEE97BECL}},{{(-3L),4L,0xC659668AL},{0x27ECB6E8L,0x903B8919L,(-1L)},{6L,(-7L),0xC659668AL},{0xE8274EACL,0xE1F44506L,0x4EAB465CL},{4L,0xC659668AL,0x7F0FCA53L},{0x27ECB6E8L,0x8C0733A2L,1L},{(-1L),0xAAC8A26EL,(-3L)},{(-2L),6L,0x4ED03EDAL},{0x124AF2F5L,(-3L),(-1L)},{1L,(-8L),0xEEE97BECL}},{{0x9279772DL,(-1L),0x9279772DL},{(-1L),0x87B510FDL,1L},{(-1L),0x7F0FCA53L,0L},{0x633BE4BDL,0x27ECB6E8L,0L},{0xAAC8A26EL,(-3L),(-5L)},{0x633BE4BDL,0x903B8919L,0x0402175AL},{2L,(-7L),(-1L)},{0xB3893D4FL,0xC995347EL,0x411C5323L},{(-3L),0x97A0EB8CL,0xDCD37698L},{4L,(-8L),0xB3893D4FL}},{{0xE1B3EFFAL,0xC659668AL,0x7F0FCA53L},{0xC995347EL,0xBB8ED630L,0x0402175AL},{0xE1B3EFFAL,2L,6L},{4L,0xEEE97BECL,0xE1F44506L},{(-5L),0xC659668AL,0L},{(-1L),0x63F4CD2EL,0xBB8ED630L},{0x7F0FCA53L,0x6D1F2783L,2L},{0xBB8ED630L,(-1L),0x4EAB465CL},{(-7L),(-7L),0x6D1F2783L},{4L,(-2L),6L}}}; + uint8_t l_959 = 0xE4L; + const uint16_t *l_1006[2]; + const uint16_t * const *l_1005 = &l_1006[1]; + union U2 l_1009 = {0x4A992D0AL}; + int16_t *l_1022 = &g_181.f4; + const int16_t l_1023 = 0L; + const uint8_t l_1024 = 0x99L; + int8_t l_1025 = 0x25L; + const int32_t l_1026[2][7][1] = {{{0x218A56C6L},{0L},{0x218A56C6L},{0L},{0x218A56C6L},{0L},{0x218A56C6L}},{{0L},{0x218A56C6L},{0L},{0x218A56C6L},{0L},{0x218A56C6L},{0L}}}; + uint8_t l_1035 = 3UL; + int8_t l_1120 = (-1L); + union U2 l_1121 = {0x9CB199ABL}; + int16_t l_1125[3]; + uint8_t l_1133 = 0xB6L; + uint16_t l_1172 = 65535UL; + uint16_t **l_1215 = &g_593; + uint32_t l_1276 = 4UL; + int16_t l_1302 = 0x0B16L; + uint16_t l_1332 = 0x101DL; + uint32_t * const *l_1364 = &g_1326; + uint32_t l_1392 = 4294967295UL; + uint32_t l_1397 = 0x52BECEE6L; + uint8_t l_1433 = 0x91L; + int i, j, k; + for (i = 0; i < 7; i++) + l_687[i] = &g_390; + for (i = 0; i < 2; i++) + l_1006[i] = &l_770.f2; + for (i = 0; i < 3; i++) + l_1125[i] = (-9L); + return (*g_201); +} + + +/* ------------------------------------------ */ +/* + * reads : g_75 g_181.f4 g_78 g_102 g_11 g_76 g_231 g_582 g_209 g_586 g_575 g_592 g_593 g_537 g_83 g_82 g_468 g_66 g_202.f1 g_71 g_77 g_16 g_261 g_202 g_649.f0 g_183 g_181 g_9 g_574 g_666 g_668 g_547 + * writes: g_75 g_181.f4 g_102 g_135 g_11 g_574 g_586 g_231 g_468 g_83 g_390 g_16 g_66 g_71 g_78 g_547 g_77 g_209 g_648 + */ +static const int32_t * func_29(int32_t ** p_30, int32_t * p_31, int32_t * p_32, uint32_t p_33) +{ /* block id: 356 */ + int32_t l_563[3]; + union U2 ***l_623[10][4][1]; + const union U1 *l_647 = &g_202; + int32_t l_660 = 0x63F79464L; + union U0 l_661 = {1UL}; + int32_t **l_667 = (void*)0; + int i, j, k; + for (i = 0; i < 3; i++) + l_563[i] = 0xBA602EADL; + for (i = 0; i < 10; i++) + { + for (j = 0; j < 4; j++) + { + for (k = 0; k < 1; k++) + l_623[i][j][k] = (void*)0; + } + } + for (g_75 = (-22); (g_75 >= (-9)); g_75 = safe_add_func_int8_t_s_s(g_75, 8)) + { /* block id: 359 */ + uint32_t l_584 = 1UL; + uint8_t l_607 = 0xA7L; + uint16_t l_610 = 0x2FA4L; + int32_t **l_637 = &g_83[6][8]; + const int32_t **l_638 = &g_16; + uint16_t *l_644 = &g_135; + union U1 *l_650 = &g_202; + union U2 l_651 = {4L}; + for (g_181.f4 = 6; (g_181.f4 >= 2); g_181.f4 -= 1) + { /* block id: 362 */ + union U2 *l_567 = &g_9[0]; + int32_t * const *l_581[1]; + int16_t l_585 = 0x9959L; + const int32_t **l_599 = &g_16; + union U1 l_633 = {0L}; + int i, j; + for (i = 0; i < 1; i++) + l_581[i] = &g_83[0][9]; + if (g_78[g_181.f4][g_181.f4]) + { /* block id: 363 */ + const int32_t *l_561[5]; + union U2 l_583 = {0x0E2EE145L}; + int i; + for (i = 0; i < 5; i++) + l_561[i] = &g_11; + for (g_102 = 0; (g_102 <= 6); g_102 += 1) + { /* block id: 366 */ + const int32_t **l_562[7][9][2] = {{{(void*)0,(void*)0},{&l_561[2],&l_561[2]},{&l_561[2],&l_561[4]},{&l_561[0],&l_561[2]},{&l_561[2],&l_561[0]},{(void*)0,&l_561[2]},{&l_561[2],&l_561[4]},{&l_561[0],(void*)0},{(void*)0,&l_561[0]}},{{&l_561[2],&l_561[2]},{&l_561[2],(void*)0},{&l_561[2],&l_561[2]},{&l_561[2],(void*)0},{&l_561[2],&l_561[2]},{&l_561[2],(void*)0},{&l_561[0],(void*)0},{&l_561[2],&l_561[4]},{&l_561[2],&l_561[2]}},{{&l_561[0],&l_561[4]},{&l_561[2],&l_561[2]},{(void*)0,&l_561[4]},{&l_561[2],&l_561[2]},{&l_561[0],(void*)0},{&l_561[0],&l_561[2]},{&l_561[2],&l_561[0]},{&l_561[2],(void*)0},{&l_561[2],&l_561[0]}},{{&l_561[2],(void*)0},{&l_561[2],&l_561[4]},{&l_561[2],(void*)0},{&l_561[0],(void*)0},{&l_561[2],&l_561[2]},{&l_561[2],&l_561[4]},{(void*)0,&l_561[2]},{&l_561[2],&l_561[0]},{&l_561[0],&l_561[2]}},{{&l_561[2],&l_561[4]},{(void*)0,(void*)0},{&l_561[0],&l_561[0]},{&l_561[2],&l_561[2]},{&l_561[2],(void*)0},{&l_561[2],&l_561[2]},{&l_561[2],(void*)0},{&l_561[2],&l_561[2]},{&l_561[2],(void*)0}},{{(void*)0,(void*)0},{&l_561[2],&l_561[0]},{&l_561[2],&l_561[2]},{(void*)0,&l_561[4]},{(void*)0,&l_561[2]},{&l_561[0],&l_561[4]},{&l_561[2],&l_561[2]},{(void*)0,(void*)0},{(void*)0,&l_561[2]}},{{&l_561[2],&l_561[0]},{&l_561[2],(void*)0},{&l_561[2],(void*)0},{&l_561[2],(void*)0},{&l_561[2],&l_561[4]},{&l_561[2],(void*)0},{(void*)0,(void*)0},{&l_561[2],&l_561[2]},{&l_561[2],&l_561[4]}}}; + int i, j, k; + l_561[2] = (g_78[g_102][(g_181.f4 + 2)] , l_561[2]); + } + for (g_135 = 0; (g_135 <= 6); g_135 += 1) + { /* block id: 385 */ + uint16_t *l_578[9] = {(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0}; + uint16_t **l_591 = &l_578[5]; + int i; + if (l_563[1]) + break; + if (((**p_30) = (**p_30))) + { /* block id: 388 */ + const uint32_t *l_570 = (void*)0; + const uint32_t *l_572 = &g_573; + const uint32_t **l_571[7][6] = {{&l_572,&l_572,&l_572,&l_572,&l_572,&l_572},{&l_572,&l_572,&l_572,&l_572,&l_572,&l_572},{&l_572,&l_572,&l_572,&l_572,&l_572,&l_572},{&l_572,&l_572,&l_572,&l_572,&l_572,&l_572},{&l_572,&l_572,&l_572,&l_572,&l_572,&l_572},{&l_572,&l_572,&l_572,&l_572,&l_572,&l_572},{&l_572,&l_572,&l_572,&l_572,&l_572,&l_572}}; + union U0 ****l_588 = &g_586; + uint8_t *l_594 = &l_583.f1; + union U2 l_595 = {0L}; + int i, j; + l_584 = (((safe_rshift_func_uint16_t_u_s((((p_32 == (g_574 = (l_570 = &g_547))) ^ ((**p_30) = (0UL & (safe_div_func_int8_t_s_s(g_76[3], g_231))))) > g_78[g_181.f4][g_181.f4]), 1)) & (l_578[6] != (func_45((safe_mul_func_uint16_t_u_u((((l_581[0] == &p_31) >= 0x707B488DL) <= g_76[3]), p_33)), g_582, l_583, &g_83[0][9], p_33) , &g_135))) && p_33); + (**p_30) &= (((g_209 , 65535UL) || l_585) && (func_45((((*l_588) = g_586) == &g_587), ((*g_593) &= (((&g_82 != &p_30) ^ (l_563[0] = ((*l_594) = (safe_mul_func_int8_t_s_s(g_575, (l_591 == g_592)))))) <= 0x9676L)), l_595, &g_83[0][9], l_584) && l_563[2])); + return (*g_537); + } + else + { /* block id: 399 */ + if (l_563[0]) + break; + } + if ((**p_30)) + { /* block id: 402 */ + return (*g_82); + } + else + { /* block id: 404 */ + (*p_32) = ((**p_30) &= 0L); + } + } + return (*p_30); + } + else + { /* block id: 410 */ + uint32_t l_601 = 4294967293UL; + int16_t l_606 = 0x86ADL; + uint32_t *l_618 = &g_547; + int16_t *l_619 = &g_77; + union U2 * const **l_622 = (void*)0; + union U2 ****l_624[6][1]; + int32_t l_625 = 7L; + int i, j; + for (i = 0; i < 6; i++) + { + for (j = 0; j < 1; j++) + l_624[i][j] = &l_623[6][0][0]; + } + (*g_537) = (*g_537); + for (g_390 = 0; (g_390 <= 6); g_390 += 1) + { /* block id: 414 */ + const union U0 *l_596[8] = {&g_181,&g_181,&g_181,&g_181,&g_181,&g_181,&g_181,&g_181}; + const union U0 **l_597 = &l_596[7]; + union U1 l_598 = {0xBA0F4ADFL}; + int32_t l_600 = (-8L); + const uint16_t l_604 = 0x3537L; + int i; + (*l_597) = l_596[7]; + if (func_55(((*p_32) && 0xDACB73B6L), l_598, func_61((+(l_563[2] & p_33)), l_599), g_202.f1)) + { /* block id: 416 */ + uint8_t l_605 = 0x27L; + l_601--; + if (l_601) + break; + l_605 = l_604; + } + else + { /* block id: 420 */ + if (l_606) + break; + if (l_607) + break; + (*p_32) = (0x66L || 251UL); + (**p_30) ^= (g_77 & l_584); + } + (**p_30) ^= l_563[0]; + (**p_30) ^= (*p_32); + } + (**p_30) = ((safe_div_func_uint8_t_u_u(l_610, (safe_div_func_int16_t_s_s((g_209 = (safe_unary_minus_func_uint8_t_u(((safe_mod_func_int16_t_s_s(((*l_619) = (((*l_618) = (safe_add_func_uint32_t_u_u(p_33, l_606))) & (l_610 >= 254UL))), 0xFB78L)) == (*p_32))))), (safe_mul_func_int16_t_s_s((((l_622 == (l_623[2][1][0] = l_623[6][0][0])) <= 5L) , (-7L)), 0x2D99L)))))) < 0x64L); + for (g_547 = 0; (g_547 <= 6); g_547 += 1) + { /* block id: 436 */ + union U1 l_634 = {0x8542DBDAL}; + union U2 l_636 = {1L}; + uint32_t l_639 = 0x3777B03DL; + for (g_71 = 0; (g_71 <= 6); g_71 += 1) + { /* block id: 439 */ + l_625 = (**p_30); + } + for (g_77 = 0; (g_77 <= 6); g_77 += 1) + { /* block id: 444 */ + union U2 l_626 = {0x7E9C8075L}; + int32_t *l_635 = &g_102; + } + return (*g_82); + } + } + } + (*l_638) = func_2(((safe_mul_func_int16_t_s_s(g_231, ((((((**l_638) , (**p_30)) <= (((*l_644) = p_33) > (safe_div_func_uint32_t_u_u(func_55(g_78[6][6], (*g_261), (*l_637), ((((g_648 = l_647) == l_650) & g_582) && 1L)), (**p_30))))) >= 0x11L) != 0x1AC1L) | 4294967290UL))) , g_649.f0), l_651, (*g_82), (*g_183), (*l_650)); + } + (**p_30) |= (func_45(((+((safe_mod_func_uint8_t_u_u(l_563[0], (safe_mul_func_int8_t_s_s((safe_add_func_int16_t_s_s((-2L), func_45((safe_mul_func_int16_t_s_s((l_660 = l_563[2]), p_33)), (*g_593), ((l_661 , (safe_add_func_uint16_t_u_u(0UL, (safe_lshift_func_uint8_t_u_u(p_33, 5))))) , (((p_33 ^ 0xBAEAL) & g_202.f1) , g_9[2])), &g_83[0][9], (*g_574)))), l_563[2])))) != 7UL)) & g_666), (**g_592), g_9[0], l_667, p_33) <= g_668); + return (*p_30); +} + + +/* ------------------------------------------ */ +/* + * reads : g_181.f4 g_209 g_202.f0 g_11 g_181.f2 g_144 g_83 g_201 g_202 g_537 + * writes: g_181.f4 g_144 g_547 g_390 g_135 g_66 g_16 g_83 + */ +static int32_t * func_36(int32_t ** p_37) +{ /* block id: 342 */ + int16_t *l_541[5] = {&g_77,&g_77,&g_77,&g_77,&g_77}; + int32_t l_542 = 1L; + int32_t l_543 = 1L; + int32_t *l_544 = &g_144; + union U0 l_545 = {0x4F299A70L}; + uint32_t *l_546 = &g_547; + int8_t *l_548 = &g_390; + uint16_t *l_549 = &g_135; + uint32_t l_550 = 1UL; + union U2 l_551[10][4] = {{{-1L},{-1L},{0x1487E6D1L},{0x1487E6D1L}},{{0x008F65D1L},{-1L},{0x1487E6D1L},{-1L}},{{-1L},{1L},{-1L},{7L}},{{0x9D8FA7ECL},{1L},{0x9D8FA7ECL},{-1L}},{{-1L},{0x008F65D1L},{-1L},{0x9D8FA7ECL}},{{-1L},{7L},{-1L},{-1L}},{{-1L},{0x008F65D1L},{-1L},{0x008F65D1L}},{{-1L},{1L},{0x008F65D1L},{0x1487E6D1L}},{{0x1487E6D1L},{0x1487E6D1L},{-1L},{-1L}},{{0x9D8FA7ECL},{-1L},{-1L},{-1L}}}; + int32_t *l_552[5][10] = {{&g_144,&l_543,&l_543,&l_542,&l_543,&l_543,&g_11,&l_545.f3,&l_542,&l_542},{&l_542,&l_545.f3,&l_543,&l_545.f3,&l_545.f3,&g_144,&l_545.f3,&g_468,&l_543,&l_542},{&g_468,&l_542,&g_144,&l_545.f3,&l_543,&g_468,&l_545.f3,&g_11,&l_545.f3,&g_468},{&l_542,&l_545.f3,&l_545.f3,&l_545.f3,&l_542,&l_545.f3,&g_11,&l_545.f3,&l_543,&g_144},{&l_542,&l_543,&g_468,&l_545.f3,&g_11,&g_468,&g_468,&l_545.f3,&g_144,&g_11}}; + uint32_t l_553 = 8UL; + uint8_t *l_554 = &g_66[0]; + uint16_t l_557[3]; + int i, j; + for (i = 0; i < 3; i++) + l_557[i] = 3UL; + (*l_544) = (((func_45(((safe_mod_func_int16_t_s_s((l_542 &= (g_181.f4 &= 0x1366L)), l_543)) & (l_543 | (((*l_544) = 0x6B1D3010L) == ((l_545 , 0xF9L) < (((*l_546) = l_543) >= ((65535UL >= ((*l_549) = (((*l_548) = g_209) || g_202.f0))) ^ l_550)))))), g_11, l_551[7][0], p_37, l_545.f3) || g_181.f2) == l_545.f0) == g_11); + l_553 ^= (*l_544); + if (g_144) + goto lbl_558; +lbl_558: + g_16 = func_2(((*l_554) = (*l_544)), l_551[8][3], (*p_37), (((safe_lshift_func_uint16_t_u_u((((&g_181 == (l_545 , &g_181)) == (*l_544)) <= 65526UL), 3)) ^ ((!((*l_544) | l_557[1])) <= (*l_544))) , l_545), (*g_201)); + (*g_537) = (*p_37); + return (*g_537); +} + + +/* ------------------------------------------ */ +/* + * reads : g_11 g_77 g_16 g_71 g_82 + * writes: + */ +static int32_t ** func_38(int16_t p_39, int8_t p_40, union U2 p_41, union U0 p_42) +{ /* block id: 19 */ + union U2 l_87 = {-6L}; + int16_t l_92 = 0x69BAL; + int32_t *l_101[5][9][5] = {{{&g_102,&g_11,&g_11,&g_11,(void*)0},{(void*)0,&g_102,(void*)0,&g_102,&g_102},{(void*)0,(void*)0,&g_102,&g_11,&g_11},{&g_11,&g_11,(void*)0,&g_11,&g_11},{&g_102,&g_11,&g_102,&g_11,&g_102},{&g_102,&g_11,&g_102,&g_11,(void*)0},{&g_11,&g_102,(void*)0,&g_11,&g_102},{&g_102,&g_102,&g_102,(void*)0,&g_11},{&g_102,&g_102,&g_11,&g_11,(void*)0}},{{&g_102,(void*)0,&g_11,&g_11,(void*)0},{&g_102,&g_102,&g_102,&g_11,&g_11},{&g_11,&g_11,&g_11,&g_11,&g_102},{&g_11,&g_102,&g_11,&g_11,&g_102},{&g_102,&g_11,&g_102,&g_102,&g_102},{(void*)0,&g_102,&g_102,&g_11,&g_11},{&g_11,&g_102,&g_102,(void*)0,&g_11},{&g_11,&g_102,(void*)0,(void*)0,&g_11},{(void*)0,&g_102,&g_102,&g_11,&g_11}},{{&g_102,&g_11,&g_102,&g_11,&g_11},{&g_102,&g_102,&g_102,&g_102,&g_11},{&g_102,&g_102,&g_11,&g_102,&g_102},{&g_11,(void*)0,&g_102,&g_102,&g_102},{&g_11,&g_11,&g_11,&g_102,&g_102},{&g_102,&g_11,&g_102,&g_102,&g_11},{&g_102,&g_102,&g_102,&g_102,(void*)0},{&g_102,(void*)0,(void*)0,&g_11,&g_102},{&g_102,&g_102,(void*)0,(void*)0,&g_11}},{{&g_102,(void*)0,&g_11,&g_102,&g_11},{&g_102,&g_102,(void*)0,&g_102,&g_102},{&g_102,(void*)0,(void*)0,&g_11,&g_102},{&g_102,&g_11,&g_11,(void*)0,&g_11},{&g_102,(void*)0,&g_102,&g_11,(void*)0},{&g_11,&g_102,&g_102,&g_102,(void*)0},{&g_11,&g_11,&g_102,&g_102,&g_102},{&g_102,&g_11,&g_11,&g_102,&g_102},{&g_102,&g_102,&g_11,&g_11,&g_102}},{{(void*)0,&g_11,&g_102,(void*)0,&g_102},{(void*)0,&g_11,&g_102,&g_102,&g_102},{&g_102,&g_102,&g_102,&g_102,&g_11},{&g_102,&g_11,&g_102,&g_11,&g_102},{&g_11,&g_11,&g_102,&g_11,&g_102},{&g_11,&g_102,&g_102,&g_11,&g_102},{(void*)0,(void*)0,(void*)0,&g_102,&g_102},{(void*)0,&g_102,(void*)0,&g_11,&g_102},{&g_11,&g_102,&g_102,&g_102,&g_102}}}; + uint32_t l_103 = 0xBDDE1E78L; + int8_t l_114 = 0xFAL; + uint32_t l_158 = 7UL; + union U0 *l_180 = &g_181; + union U1 l_193 = {1L}; + int32_t *l_196 = &g_102; + uint32_t l_212 = 0xCC8533B4L; + uint16_t l_263 = 0x3F35L; + int32_t **l_294 = (void*)0; + int32_t ***l_295 = (void*)0; + int32_t ***l_296 = (void*)0; + int32_t ***l_297 = &g_82; + const int32_t **l_298[9] = {&g_16,&g_16,&g_16,&g_16,&g_16,&g_16,&g_16,&g_16,&g_16}; + union U2 **l_324 = &g_282; + union U2 **l_325 = &g_282; + union U1 l_467 = {0x564CF22EL}; + uint32_t l_497 = 0xAD274BE0L; + int32_t l_502[2]; + int16_t l_536 = 7L; + int i, j, k; + for (i = 0; i < 2; i++) + l_502[i] = 1L; + l_103 |= ((((l_87 , p_42) , p_42) , ((g_11 != g_11) != (safe_lshift_func_uint16_t_u_s((g_77 , (safe_mul_func_int8_t_s_s((l_92 < l_92), (safe_mul_func_uint16_t_u_u((safe_lshift_func_uint8_t_u_u(((safe_div_func_int32_t_s_s((safe_div_func_int16_t_s_s((((p_41.f0 = p_42.f4) | (*g_16)) == 0xF910L), g_71)), 0xBA0E3889L)) | 0UL), l_92)), l_92))))), 9)))) ^ 0x556CL); + return (*l_297); +} + + +/* ------------------------------------------ */ +/* + * reads : + * writes: + */ +static int8_t func_45(uint8_t p_46, uint16_t p_47, union U2 p_48, int32_t ** p_49, const uint32_t p_50) +{ /* block id: 16 */ + return p_50; +} + + +/* ------------------------------------------ */ +/* + * reads : g_71 g_78 + * writes: g_71 g_78 + */ +static int8_t func_55(uint8_t p_56, union U1 p_57, int32_t * p_58, uint8_t p_59) +{ /* block id: 12 */ + int16_t l_69 = 1L; + int32_t *l_70[8] = {&g_11,&g_11,&g_11,&g_11,&g_11,&g_11,&g_11,&g_11}; + int8_t l_74[4][7] = {{4L,4L,4L,4L,4L,4L,4L},{5L,(-5L),5L,(-5L),5L,(-5L),5L},{4L,4L,4L,4L,4L,4L,4L},{5L,(-5L),5L,(-5L),5L,(-5L),5L}}; + int32_t l_81 = 1L; + int i, j; + g_71++; + ++g_78[2][2]; + return l_81; +} + + +/* ------------------------------------------ */ +/* + * reads : g_66 + * writes: g_16 g_66 + */ +static int32_t * func_61(int16_t p_62, const int32_t ** p_63) +{ /* block id: 8 */ + int32_t *l_64 = &g_11; + int32_t *l_65[1]; + int i; + for (i = 0; i < 1; i++) + l_65[i] = &g_11; + (*p_63) = l_64; + ++g_66[0]; + return l_64; +} + + + + +/* ---------------------------------------- */ +int main (int argc, char* argv[]) +{ + int i, j; + int print_hash_value = 0; + if (argc == 2 && strcmp(argv[1], "1") == 0) print_hash_value = 1; + platform_main_begin(); + crc32_gentab(); + func_1(); + for (i = 0; i < 5; i++) + { + transparent_crc(g_9[i].f0, "g_9[i].f0", print_hash_value); + if (print_hash_value) printf("index = [%d]\n", i); + + } + transparent_crc(g_11, "g_11", print_hash_value); + for (i = 0; i < 1; i++) + { + transparent_crc(g_66[i], "g_66[i]", print_hash_value); + if (print_hash_value) printf("index = [%d]\n", i); + + } + transparent_crc(g_71, "g_71", print_hash_value); + transparent_crc(g_75, "g_75", print_hash_value); + for (i = 0; i < 5; i++) + { + transparent_crc(g_76[i], "g_76[i]", print_hash_value); + if (print_hash_value) printf("index = [%d]\n", i); + + } + transparent_crc(g_77, "g_77", print_hash_value); + for (i = 0; i < 7; i++) + { + for (j = 0; j < 9; j++) + { + transparent_crc(g_78[i][j], "g_78[i][j]", print_hash_value); + if (print_hash_value) printf("index = [%d][%d]\n", i, j); + + } + } + transparent_crc(g_102, "g_102", print_hash_value); + transparent_crc(g_135, "g_135", print_hash_value); + transparent_crc(g_144, "g_144", print_hash_value); + transparent_crc(g_160, "g_160", print_hash_value); + transparent_crc(g_181.f2, "g_181.f2", print_hash_value); + transparent_crc(g_181.f4, "g_181.f4", print_hash_value); + transparent_crc(g_202.f0, "g_202.f0", print_hash_value); + transparent_crc(g_202.f1, "g_202.f1", print_hash_value); + transparent_crc(g_209, "g_209", print_hash_value); + transparent_crc(g_211, "g_211", print_hash_value); + transparent_crc(g_231, "g_231", print_hash_value); + transparent_crc(g_328, "g_328", print_hash_value); + transparent_crc(g_390, "g_390", print_hash_value); + transparent_crc(g_468, "g_468", print_hash_value); + transparent_crc(g_547, "g_547", print_hash_value); + transparent_crc(g_573, "g_573", print_hash_value); + transparent_crc(g_575, "g_575", print_hash_value); + transparent_crc(g_582, "g_582", print_hash_value); + transparent_crc(g_649.f0, "g_649.f0", print_hash_value); + transparent_crc(g_649.f1, "g_649.f1", print_hash_value); + transparent_crc(g_666, "g_666", print_hash_value); + transparent_crc(g_668, "g_668", print_hash_value); + transparent_crc(g_756, "g_756", print_hash_value); + transparent_crc(g_860, "g_860", print_hash_value); + transparent_crc(g_894, "g_894", print_hash_value); + transparent_crc(g_947, "g_947", print_hash_value); + for (i = 0; i < 5; i++) + { + for (j = 0; j < 3; j++) + { + transparent_crc(g_966[i][j], "g_966[i][j]", print_hash_value); + if (print_hash_value) printf("index = [%d][%d]\n", i, j); + + } + } + for (i = 0; i < 9; i++) + { + for (j = 0; j < 9; j++) + { + transparent_crc(g_1059[i][j].f0, "g_1059[i][j].f0", print_hash_value); + transparent_crc(g_1059[i][j].f1, "g_1059[i][j].f1", print_hash_value); + if (print_hash_value) printf("index = [%d][%d]\n", i, j); + + } + } + transparent_crc(g_1124, "g_1124", print_hash_value); + transparent_crc(g_1254, "g_1254", print_hash_value); + transparent_crc(g_1294, "g_1294", print_hash_value); + transparent_crc(g_1418, "g_1418", print_hash_value); + transparent_crc(g_1501, "g_1501", print_hash_value); + transparent_crc(g_1597, "g_1597", print_hash_value); + transparent_crc(g_1613, "g_1613", print_hash_value); + for (i = 0; i < 1; i++) + { + transparent_crc(g_1665[i], "g_1665[i]", print_hash_value); + if (print_hash_value) printf("index = [%d]\n", i); + + } + platform_main_end(crc32_context ^ 0xFFFFFFFFUL, print_hash_value); + return 0; +} + +/************************ statistics ************************* +XXX max struct depth: 0 +breakdown: + depth: 0, occurrence: 592 +XXX total union variables: 93 + +XXX non-zero bitfields defined in structs: 1 +XXX zero bitfields defined in structs: 0 +XXX const bitfields defined in structs: 0 +XXX volatile bitfields defined in structs: 0 +XXX structs with bitfields in the program: 68 +breakdown: + indirect level: 0, occurrence: 51 + indirect level: 1, occurrence: 5 + indirect level: 2, occurrence: 5 + indirect level: 3, occurrence: 5 + indirect level: 4, occurrence: 2 +XXX full-bitfields structs in the program: 0 +breakdown: +XXX times a bitfields struct's address is taken: 23 +XXX times a bitfields struct on LHS: 4 +XXX times a bitfields struct on RHS: 99 +XXX times a single bitfield on LHS: 18 +XXX times a single bitfield on RHS: 43 + +XXX max expression depth: 41 +breakdown: + depth: 1, occurrence: 83 + depth: 2, occurrence: 20 + depth: 6, occurrence: 1 + depth: 7, occurrence: 2 + depth: 9, occurrence: 1 + depth: 16, occurrence: 1 + depth: 17, occurrence: 1 + depth: 19, occurrence: 1 + depth: 20, occurrence: 1 + depth: 23, occurrence: 1 + depth: 24, occurrence: 1 + depth: 25, occurrence: 1 + depth: 26, occurrence: 1 + depth: 35, occurrence: 1 + depth: 41, occurrence: 1 + +XXX total number of pointers: 403 + +XXX times a variable address is taken: 932 +XXX times a pointer is dereferenced on RHS: 354 +breakdown: + depth: 1, occurrence: 286 + depth: 2, occurrence: 54 + depth: 3, occurrence: 10 + depth: 4, occurrence: 4 +XXX times a pointer is dereferenced on LHS: 297 +breakdown: + depth: 1, occurrence: 272 + depth: 2, occurrence: 21 + depth: 3, occurrence: 4 +XXX times a pointer is compared with null: 26 +XXX times a pointer is compared with address of another variable: 7 +XXX times a pointer is compared with another pointer: 10 +XXX times a pointer is qualified to be dereferenced: 6446 + +XXX max dereference level: 4 +breakdown: + level: 0, occurrence: 0 + level: 1, occurrence: 1057 + level: 2, occurrence: 375 + level: 3, occurrence: 48 + level: 4, occurrence: 15 +XXX number of pointers point to pointers: 164 +XXX number of pointers point to scalars: 201 +XXX number of pointers point to structs: 0 +XXX percent of pointers has null in alias set: 31 +XXX average alias set size: 1.41 + +XXX times a non-volatile is read: 2062 +XXX times a non-volatile is write: 900 +XXX times a volatile is read: 0 +XXX times read thru a pointer: 0 +XXX times a volatile is write: 0 +XXX times written thru a pointer: 0 +XXX times a volatile is available for access: 0 +XXX percentage of non-volatile access: 100 + +XXX forward jumps: 1 +XXX backward jumps: 12 + +XXX stmts: 80 +XXX max block depth: 5 +breakdown: + depth: 0, occurrence: 27 + depth: 1, occurrence: 4 + depth: 2, occurrence: 7 + depth: 3, occurrence: 14 + depth: 4, occurrence: 14 + depth: 5, occurrence: 14 + +XXX percentage a fresh-made variable is used: 18.2 +XXX percentage an existing variable is used: 81.8 +FYI: the random generator makes assumptions about the integer size. See platform.info for more details. +********************* end of statistics **********************/ + diff --git a/tests/fuzz/7.c.txt b/tests/fuzz/7.c.txt new file mode 100644 index 00000000..9d4c7690 --- /dev/null +++ b/tests/fuzz/7.c.txt @@ -0,0 +1 @@ +checksum = 40E796EF diff --git a/tests/fuzz/8.c b/tests/fuzz/8.c new file mode 100644 index 00000000..2dbaa5b2 --- /dev/null +++ b/tests/fuzz/8.c @@ -0,0 +1,2214 @@ +/* + * This is a RANDOMLY GENERATED PROGRAM. + * + * Generator: csmith 2.2.0 + * Git version: a8697aa + * Options: --no-volatiles --no-math64 --no-packed-struct + * Seed: 958095120 + */ + +#include "csmith.h" + + +static long __undefined; + +/* --- Struct/Union Declarations --- */ +struct S0 { + signed f0 : 22; + unsigned f1 : 19; + const unsigned f2 : 9; +}; + +struct S1 { + uint8_t f0; + unsigned f1 : 17; +}; + +struct S2 { + const struct S0 f0; + int32_t f1; + struct S0 f2; + uint16_t f3; + const uint32_t f4; + uint32_t f5; + struct S1 f6; + const struct S0 f7; + const uint32_t f8; +}; + +union U3 { + int8_t f0; + signed f1 : 1; + uint8_t f2; + int8_t f3; +}; + +/* --- GLOBAL VARIABLES --- */ +static int32_t g_8 = 0xD069F498L; +static int16_t g_54[10][9] = {{0L,0xDAE1L,(-1L),(-6L),0x7DA8L,(-6L),0x40C8L,0xDAE1L,0x1D9DL},{0xB79AL,(-1L),(-1L),0xD900L,(-6L),0xD17BL,1L,0L,1L},{0xB79AL,9L,0x0DE2L,1L,0L,(-6L),(-6L),0L,0x890FL},{0xD17BL,0xD900L,0xD17BL,(-6L),1L,(-6L),0xD900L,9L,0x40C8L},{0x7DA8L,0x40C8L,0x5FE9L,(-6L),6L,0xB79AL,1L,0x4C3DL,0L},{1L,(-1L),0xB79AL,0x4C3DL,0x1D9DL,0x1D9DL,0x5FE9L,(-6L),1L},{0x1D9DL,0x0DE2L,0L,(-1L),0xD17BL,1L,(-6L),(-6L),1L},{(-6L),0xD17BL,0x4C3DL,0x890FL,1L,0x890FL,0x4C3DL,0xD17BL,0xD900L},{(-1L),0x5FE9L,0x5FE9L,0L,0x890FL,0x0DE2L,(-1L),0xD900L,0x7DA8L},{1L,0xB79AL,6L,0xDAE1L,0xDAE1L,0L,0x890FL,(-6L),0x0DE2L}}; +static struct S0 g_60 = {1766,696,10}; +static uint8_t g_67 = 7UL; +static struct S2 g_70 = {{-1862,222,13},0xB8BF5144L,{-1033,470,13},0x4A9FL,7UL,0x06D673C0L,{0x29L,4},{249,44,17},0UL}; +static uint16_t g_79 = 0UL; +static const struct S0 *g_113 = &g_70.f7; +static const struct S0 ** const g_112 = &g_113; +static int32_t *g_125 = &g_8; +static int32_t **g_124 = &g_125; +static uint8_t g_141 = 0x42L; +static struct S1 *g_156[4][8][8] = {{{(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0},{(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0},{(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0},{(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0},{(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0},{(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0},{(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0},{(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0}},{{(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0},{(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0},{(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0},{(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0},{(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0},{(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0},{(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0},{(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0}},{{(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0},{(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0},{(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0},{(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0},{(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0},{(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0},{(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0},{(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0}},{{(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0},{(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0},{(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0},{(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0},{(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0},{(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0},{(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0},{(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0,(void*)0}}}; +static uint8_t g_203[10] = {0x5BL,0x5BL,0x5BL,0x5BL,0x5BL,0x5BL,0x5BL,0x5BL,0x5BL,0x5BL}; +static uint32_t g_217[5] = {0xFF553064L,0xFF553064L,0xFF553064L,0xFF553064L,0xFF553064L}; +static int16_t g_234 = 0x7821L; +static uint16_t g_235 = 0xABFCL; +static int16_t g_252 = 0xF752L; +static int16_t g_254[10] = {0x0379L,0x0379L,0x0379L,0x0379L,0x0379L,0x0379L,0x0379L,0x0379L,0x0379L,0x0379L}; +static uint16_t g_255 = 5UL; +static struct S0 *g_259 = &g_70.f2; +static struct S0 **g_258 = &g_259; +static const int8_t g_281 = 0x46L; +static int32_t g_285[7] = {0L,0L,0L,0L,0L,0L,0L}; +static union U3 g_304 = {-8L}; +static uint32_t g_351 = 0x1A7B9ABFL; +static struct S1 g_361 = {252UL,209}; +static uint8_t g_371[6] = {0x1EL,0x1EL,0x1EL,1UL,1UL,1UL}; +static uint8_t g_389[7] = {0x18L,0x18L,0x18L,0x18L,0x18L,0x18L,0x18L}; +static uint8_t g_397[9][9] = {{1UL,0xA8L,1UL,0xA8L,1UL,0xA8L,1UL,0xA8L,1UL},{255UL,255UL,255UL,255UL,255UL,255UL,255UL,255UL,255UL},{1UL,0xA8L,1UL,0xA8L,1UL,0xA8L,1UL,0xA8L,1UL},{255UL,255UL,255UL,255UL,255UL,255UL,255UL,255UL,255UL},{1UL,0xA8L,1UL,0xA8L,1UL,0xA8L,1UL,0xA8L,1UL},{255UL,255UL,255UL,255UL,255UL,255UL,255UL,255UL,255UL},{1UL,0xA8L,1UL,0xA8L,1UL,0xA8L,1UL,0xA8L,1UL},{255UL,255UL,255UL,255UL,255UL,255UL,255UL,255UL,255UL},{1UL,0xA8L,1UL,0xA8L,1UL,0xA8L,1UL,0xA8L,1UL}}; +static const int16_t g_449 = 0x06B9L; +static union U3 g_489 = {1L}; +static const union U3 g_524 = {0xFCL}; +static struct S1 **g_538[1] = {(void*)0}; +static uint16_t g_621[10][2][6] = {{{65533UL,65533UL,0UL,0x5037L,0xE95EL,65533UL},{4UL,65533UL,0x38D8L,65533UL,0xE95EL,65527UL}},{{65527UL,0UL,0x5037L,0x68F9L,0x68F9L,4UL},{0xE95EL,0UL,0x68F9L,65527UL,0x68F9L,0xE95EL}},{{0x5037L,0xE95EL,0UL,0UL,4UL,0x38D8L},{65533UL,0xE95EL,65527UL,0x5037L,4UL,0x240AL}},{{0x68F9L,0x68F9L,0x5037L,0x38D8L,0x5037L,0x68F9L},{65527UL,0x68F9L,0xE95EL,0x68F9L,0x5037L,0UL}},{{0UL,4UL,65533UL,0x240AL,0x240AL,65527UL},{0x5037L,0x5037L,0x240AL,0UL,0x240AL,0x5037L}},{{0x38D8L,0x5037L,4UL,4UL,65527UL,0xE95EL},{0x68F9L,0x240AL,0UL,0x38D8L,65527UL,65533UL}},{{0x240AL,0x240AL,0x38D8L,0xE95EL,0x38D8L,0x240AL},{0UL,65527UL,0x5037L,0x240AL,0x38D8L,4UL}},{{0x5037L,65527UL,0x68F9L,65533UL,65533UL,0UL},{0x38D8L,0x38D8L,65533UL,4UL,65533UL,0x38D8L}},{{0x68F9L,0x38D8L,65527UL,65527UL,0UL,0x240AL},{0x240AL,65533UL,4UL,0xE95EL,0UL,0x68F9L}},{{0UL,65533UL,0xE95EL,0x5037L,0xE95EL,0UL},{4UL,0UL,0x38D8L,65533UL,0x68F9L,65527UL}}}; +static const union U3 *g_664 = &g_489; +static const union U3 **g_663[6][1][8] = {{{&g_664,&g_664,&g_664,&g_664,&g_664,&g_664,&g_664,&g_664}},{{&g_664,&g_664,&g_664,&g_664,&g_664,&g_664,&g_664,&g_664}},{{&g_664,&g_664,&g_664,&g_664,&g_664,&g_664,&g_664,&g_664}},{{&g_664,&g_664,&g_664,&g_664,&g_664,&g_664,&g_664,&g_664}},{{&g_664,&g_664,&g_664,&g_664,&g_664,&g_664,&g_664,&g_664}},{{&g_664,&g_664,&g_664,&g_664,&g_664,&g_664,&g_664,&g_664}}}; +static int16_t g_739[6] = {3L,3L,0x0F38L,4L,0x0F38L,3L}; +static int8_t g_745 = (-1L); +static int32_t g_747 = 1L; +static uint32_t g_829[2] = {0x935C6A21L,0x935C6A21L}; +static int32_t g_864[9][5][1] = {{{0xF6C99171L},{0x23E90A78L},{0x23E90A78L},{0x23E90A78L},{0xF6C99171L}},{{0xF6C99171L},{0xF6C99171L},{0xFB433239L},{0x23E90A78L},{0xFB433239L}},{{0x23E90A78L},{0xF6C99171L},{0x23E90A78L},{0xFB433239L},{0xFB433239L}},{{0xFB433239L},{0x23E90A78L},{0x23E90A78L},{0x23E90A78L},{0xF6C99171L}},{{0xFB433239L},{0xF6C99171L},{0xFB433239L},{0x23E90A78L},{0xFB433239L}},{{0xF6C99171L},{0xF6C99171L},{0x23E90A78L},{0xFB433239L},{0xFB433239L}},{{0xFB433239L},{0x23E90A78L},{0x23E90A78L},{0x23E90A78L},{0xF6C99171L}},{{0xFB433239L},{0xF6C99171L},{0x23E90A78L},{0x23E90A78L},{0xFB433239L}},{{0xF6C99171L},{0xF6C99171L},{0xF6C99171L},{0xFB433239L},{0xFB433239L}}}; +static const struct S2 g_873[10] = {{{-1400,335,11},0xAFE2933CL,{1507,179,3},0UL,0xD51EF24AL,4294967287UL,{0UL,25},{-1032,20,21},0x5CBAB4F1L},{{-1400,335,11},0xAFE2933CL,{1507,179,3},0UL,0xD51EF24AL,4294967287UL,{0UL,25},{-1032,20,21},0x5CBAB4F1L},{{1703,485,7},0x9C6C5086L,{1741,654,6},2UL,1UL,1UL,{255UL,223},{-1994,192,10},0UL},{{-350,118,17},0x8ED3ED5FL,{-1139,317,18},0x4A33L,0x123C372FL,1UL,{1UL,331},{-1160,101,7},0x29F7AFD7L},{{1703,485,7},0x9C6C5086L,{1741,654,6},2UL,1UL,1UL,{255UL,223},{-1994,192,10},0UL},{{1703,485,7},0x9C6C5086L,{1741,654,6},2UL,1UL,1UL,{255UL,223},{-1994,192,10},0UL},{{1703,485,7},0x9C6C5086L,{1741,654,6},2UL,1UL,1UL,{255UL,223},{-1994,192,10},0UL},{{-350,118,17},0x8ED3ED5FL,{-1139,317,18},0x4A33L,0x123C372FL,1UL,{1UL,331},{-1160,101,7},0x29F7AFD7L},{{1838,85,6},-1L,{-806,565,13},0x04AFL,0x3473F18AL,0xFFEB4ACFL,{0UL,71},{1706,355,4},1UL},{{-350,118,17},0x8ED3ED5FL,{-1139,317,18},0x4A33L,0x123C372FL,1UL,{1UL,331},{-1160,101,7},0x29F7AFD7L}}; +static uint8_t *g_904 = (void*)0; +static uint8_t **g_903 = &g_904; +static int8_t g_921 = (-1L); +static uint8_t ***g_962 = &g_903; +static int8_t g_973 = 0x25L; +static uint32_t g_987[1][5][1] = {{{0xCEE53CD7L},{0xCEE53CD7L},{0xCEE53CD7L},{0xCEE53CD7L},{0xCEE53CD7L}}}; +static struct S2 g_994 = {{1271,119,9},0x1F922EA6L,{-338,152,14},65535UL,9UL,4294967295UL,{0x06L,47},{1040,71,21},0x0F9F6E59L}; +static int8_t g_1120[4] = {1L,1L,1L,1L}; +static const uint32_t **g_1134 = (void*)0; +static union U3 **g_1183 = (void*)0; +static int16_t g_1191 = 7L; +static struct S2 *g_1269 = (void*)0; +static struct S1 ***g_1322 = &g_538[0]; +static struct S1 ****g_1321[3] = {&g_1322,&g_1322,&g_1322}; +static uint32_t g_1371 = 0x6AB0ECFCL; +static int8_t ** const g_1385 = (void*)0; +static struct S2 g_1439 = {{-1864,157,10},0L,{995,67,13},65526UL,0xEDB13D73L,0x2334F1D6L,{0UL,199},{1788,370,3},0xDA46817DL}; +static int32_t g_1441 = 0x14E17A81L; +static int16_t *g_1525 = &g_739[5]; +static int16_t **g_1524 = &g_1525; +static const int16_t **g_1528 = (void*)0; +static const int16_t **g_1529 = (void*)0; +static union U3 *g_1634 = &g_489; +static struct S2 g_1672 = {{-1762,333,7},0xF4A56E7FL,{419,337,19},0x9753L,0xC99B31FFL,0x7AE3A617L,{255UL,192},{1333,52,11},5UL}; + + +/* --- FORWARD DECLARATIONS --- */ +static uint32_t func_1(void); +static uint16_t func_2(int32_t p_3, uint8_t p_4, int16_t p_5, union U3 p_6, const uint32_t p_7); +static union U3 func_10(int8_t p_11, struct S1 p_12, uint32_t p_13, uint16_t p_14); +static struct S1 func_19(int16_t p_20, union U3 p_21, uint8_t p_22, uint8_t p_23); +static uint16_t func_24(uint32_t p_25, struct S0 p_26, union U3 p_27, uint32_t p_28, const union U3 p_29); +static struct S0 func_30(const int8_t p_31, union U3 p_32); +static union U3 func_33(int8_t p_34, int16_t p_35, int32_t p_36); +static struct S1 * func_39(int32_t p_40, uint16_t p_41, struct S1 * p_42, struct S1 * p_43); +static struct S1 * func_45(int32_t p_46, uint32_t p_47); +static struct S0 * func_55(int16_t p_56, struct S0 * p_57); + + +/* --- FUNCTIONS --- */ +/* ------------------------------------------ */ +/* + * reads : g_8 g_54 g_60.f0 g_70 g_79 g_112 g_124 g_125 g_141 g_156 g_67 g_60.f2 g_203 g_217 g_235 g_255 g_258 g_252 g_259 g_281 g_285 g_304 g_304.f3 g_304.f2 g_351 g_361 g_371 g_254 g_389 g_397 g_234 g_449 g_489 g_113 g_524.f2 g_489.f2 g_664 g_489.f3 g_489.f0 g_873.f2.f2 g_739 g_903 g_873.f6.f0 g_921 g_873.f8 g_873.f4 g_873.f0.f0 g_873.f2.f1 g_973 g_987 g_829 g_994.f4 g_994.f2.f2 g_994.f8 g_873.f5 g_747 g_873.f7.f0 g_994.f0.f2 g_60.f1 g_994.f2.f0 g_745 g_1134 g_994.f1 g_621 g_864 g_1191 g_1321 g_994.f7.f0 g_1371 g_1524 g_1528 g_1525 g_663 g_1439.f2.f2 g_1439.f0.f1 g_873.f3 g_524.f3 g_873.f0.f1 g_1120 g_1439.f6.f0 g_1439.f0.f0 g_1672.f0.f0 g_994.f0.f1 g_1439.f6.f1 g_1672.f6.f1 + * writes: g_54 g_67 g_79 g_70.f2.f0 g_70.f6.f0 g_70.f6 g_60.f0 g_70.f5 g_141 g_125 g_203 g_217 g_70.f1 g_235 g_234 g_255 g_252 g_285 g_156 g_70.f3 g_351 g_254 g_304.f0 g_371 g_389 g_361 g_304.f3 g_489.f1 g_113 g_538 g_489.f2 g_489.f3 g_962 g_489 g_397 g_994.f2.f0 g_1134 g_8 g_304 g_745 g_1371 g_663 g_829 g_1524 g_1529 g_739 g_994.f3 g_1439.f3 g_1191 g_258 g_1634 + */ +static uint32_t func_1(void) +{ /* block id: 0 */ + uint16_t l_9 = 0xCD40L; + int16_t l_38[6] = {(-6L),(-6L),(-6L),(-6L),(-6L),(-6L)}; + const union U3 l_312[4] = {{0xF3L},{0xF3L},{0xF3L},{0xF3L}}; + int8_t *l_360 = &g_304.f0; + union U3 l_754[4] = {{0x5FL},{0x5FL},{0x5FL},{0x5FL}}; + struct S0 l_1127 = {-277,713,18}; + struct S1 l_1192 = {0xBBL,139}; + uint8_t ***l_1201 = &g_903; + struct S0 **l_1216[8] = {&g_259,&g_259,&g_259,&g_259,&g_259,&g_259,&g_259,&g_259}; + int32_t *l_1225 = &g_285[4]; + int32_t l_1234 = 0xDF42B7A1L; + union U3 l_1276 = {0x45L}; + uint32_t l_1323[6]; + int8_t l_1345 = 0x20L; + union U3 l_1347 = {0x5CL}; + union U3 *l_1352 = (void*)0; + int16_t l_1369[10][2] = {{0xA13FL,0L},{0xA13FL,0xEBB6L},{(-4L),(-4L)},{0xA13FL,(-4L)},{(-4L),0L},{0L,(-4L)},{0L,0L},{(-4L),0xA13FL},{0L,0xA13FL},{0xA13FL,0L}}; + const uint8_t l_1401[8] = {248UL,248UL,248UL,248UL,248UL,248UL,248UL,248UL}; + const uint32_t l_1419 = 0xEC2B74D4L; + int32_t l_1449 = 0x929224DFL; + int32_t l_1451 = (-8L); + int32_t l_1452[2]; + int32_t l_1453 = 1L; + const union U3 l_1493 = {0x90L}; + const union U3 l_1556 = {0L}; + uint32_t l_1574 = 0x9C50339FL; + struct S2 *l_1671 = &g_1672; + const int32_t *l_1719 = &l_1453; + const int32_t **l_1718[9] = {&l_1719,&l_1719,&l_1719,&l_1719,&l_1719,&l_1719,&l_1719,&l_1719,&l_1719}; + int i, j; + for (i = 0; i < 6; i++) + l_1323[i] = 1UL; + for (i = 0; i < 2; i++) + l_1452[i] = 0xF2D303A9L; + if ((func_2(g_8, l_9, l_9, (l_754[3] = func_10(((*l_360) = ((safe_add_func_int32_t_s_s(g_8, ((safe_mul_func_int8_t_s_s(((func_19((func_24(l_9, func_30(l_9, func_33((0xE55B65ABL != (safe_unary_minus_func_int16_t_s(((g_8 , 0xB5BFL) == l_38[0])))), g_8, l_9)), g_304, g_304.f3, l_312[1]) > (-7L)), g_304, g_281, l_38[1]) , 0x6988L) > 1L), 0xA3L)) & g_70.f4))) , l_312[1].f3)), g_361, l_312[1].f3, g_70.f2.f2)), l_312[1].f3) != l_312[1].f2)) + { /* block id: 684 */ + struct S1 ***l_1110 = (void*)0; + struct S1 ****l_1109 = &l_1110; + uint32_t l_1115 = 4294967289UL; + uint16_t *l_1123 = &g_994.f3; + union U3 *l_1126 = &g_304; + uint32_t *l_1128 = (void*)0; + uint32_t *l_1129 = &g_829[1]; + int32_t *l_1130 = &g_994.f1; + struct S0 l_1187 = {1711,690,10}; + int32_t l_1233 = 6L; + int32_t l_1235 = 9L; + uint16_t l_1240 = 0x94D0L; + const union U3 l_1247 = {0x97L}; + int8_t l_1284 = (-3L); + struct S1 l_1294 = {251UL,278}; + int8_t l_1301[2]; + int8_t **l_1316 = &l_360; + int8_t ***l_1315 = &l_1316; + union U3 l_1324 = {0xAFL}; + union U3 l_1344 = {0L}; + int i; + for (i = 0; i < 2; i++) + l_1301[i] = 5L; + if (l_312[1].f2) + { /* block id: 689 */ + uint16_t l_1136 = 0x2987L; + for (g_252 = 0; (g_252 != 2); g_252++) + { /* block id: 692 */ + int32_t l_1133 = (-1L); + const uint32_t ***l_1135 = &g_1134; + int32_t *l_1137 = &g_8; + if (l_38[3]) + break; + (*l_1137) |= (l_1133 , ((((*l_1135) = g_1134) != (void*)0) || l_1136)); + } + } + else + { /* block id: 697 */ + union U3 l_1164 = {1L}; + int16_t *l_1180 = &l_38[0]; + int16_t **l_1179 = &l_1180; + union U3 **l_1190 = &l_1126; + int32_t * const l_1221[7][10] = {{&g_8,(void*)0,&g_8,(void*)0,&g_8,(void*)0,&g_8,(void*)0,&g_8,(void*)0},{&g_8,(void*)0,&g_8,(void*)0,&g_8,(void*)0,&g_8,(void*)0,&g_8,(void*)0},{&g_8,(void*)0,&g_8,(void*)0,&g_8,(void*)0,&g_8,(void*)0,&g_8,(void*)0},{&g_8,(void*)0,&g_8,(void*)0,&g_8,(void*)0,&g_8,(void*)0,&g_8,(void*)0},{&g_8,(void*)0,&g_8,(void*)0,&g_8,(void*)0,&g_8,(void*)0,&g_8,(void*)0},{&g_8,(void*)0,&g_8,(void*)0,&g_8,(void*)0,&g_8,(void*)0,&g_8,(void*)0},{&g_8,(void*)0,&g_8,(void*)0,&g_8,(void*)0,&g_8,(void*)0,&g_8,(void*)0}}; + struct S1 l_1223 = {0xEBL,121}; + struct S0 l_1248 = {1351,604,9}; + int16_t l_1279[4]; + const struct S2 l_1314 = {{1802,353,14},0x3F82D2C5L,{-600,686,5},0x3667L,0x9173B856L,1UL,{0x91L,155},{-1532,265,3},2UL}; + union U3 l_1346 = {0xBAL}; + int16_t l_1348 = 0xD08AL; + struct S1 l_1349[4][4][7] = {{{{6UL,66},{0x30L,289},{0UL,48},{0x51L,94},{0UL,48},{0x84L,29},{6UL,66}},{{0xE0L,225},{0xE0L,225},{0xE0L,225},{255UL,294},{255UL,294},{255UL,294},{0xE0L,225}},{{0UL,48},{0x84L,29},{6UL,66},{0x84L,29},{0UL,48},{0x30L,289},{0UL,54}},{{0xE0L,225},{0xE0L,225},{1UL,285},{255UL,294},{1UL,285},{0xE0L,225},{1UL,285}}},{{{6UL,66},{0x51L,94},{6UL,66},{0x30L,289},{6UL,66},{0x30L,289},{0UL,48}},{{1UL,285},{0xE0L,225},{1UL,285},{0xE0L,225},{255UL,294},{255UL,294},{255UL,294}},{{0UL,48},{0x84L,29},{0UL,54},{0x30L,289},{0UL,54},{0x51L,94},{0UL,48}},{{1UL,285},{1UL,285},{1UL,285},{0xE0L,225},{0xE0L,225},{0xE0L,225},{1UL,285}}},{{{0UL,54},{0x51L,94},{0UL,48},{0x51L,94},{0UL,54},{0x84L,29},{6UL,66}},{{1UL,285},{1UL,285},{255UL,294},{0xE0L,225},{255UL,294},{1UL,285},{255UL,294}},{{0UL,48},{0x30L,289},{0UL,48},{0x84L,29},{0UL,48},{0x84L,29},{0UL,54}},{{255UL,294},{1UL,285},{255UL,294},{1UL,285},{0xE0L,225},{0xE0L,225},{0xE0L,225}}},{{{0UL,54},{0x51L,94},{6UL,66},{0x84L,29},{6UL,66},{0x30L,289},{0UL,54}},{{255UL,294},{255UL,294},{255UL,294},{1UL,285},{1UL,285},{1UL,285},{255UL,294}},{{6UL,66},{0x30L,289},{0UL,54},{0x30L,289},{6UL,66},{0x51L,94},{0UL,48}},{{255UL,294},{255UL,294},{0xE0L,225},{1UL,285},{1UL,285},{255UL,294},{0xE0L,225}}}}; + int i, j, k; + for (i = 0; i < 4; i++) + l_1279[i] = (-6L); + for (l_1115 = 0; (l_1115 != 57); l_1115 = safe_add_func_int8_t_s_s(l_1115, 2)) + { /* block id: 700 */ + int16_t l_1163 = 0x7843L; + struct S1 l_1173 = {4UL,105}; + int16_t **l_1182 = (void*)0; + union U3 l_1188 = {-1L}; + const union U3 l_1189 = {9L}; + uint8_t ** const l_1200 = (void*)0; + uint8_t ** const *l_1199[2]; + uint8_t ** const **l_1198 = &l_1199[1]; + int32_t l_1236 = 1L; + int32_t l_1237 = 4L; + int16_t l_1239[5] = {1L,1L,1L,1L,1L}; + const int32_t l_1262 = 0x1349BCC1L; + int i; + for (i = 0; i < 2; i++) + l_1199[i] = &l_1200; + } + for (g_234 = (-12); (g_234 > (-8)); ++g_234) + { /* block id: 787 */ + int32_t l_1272 = 0x5F87868CL; + uint8_t *l_1273 = &l_754[3].f2; + union U3 l_1281 = {0x49L}; + const uint32_t *l_1328 = (void*)0; + const uint32_t **l_1327 = &l_1328; + int32_t *l_1329[2][4]; + int i, j; + for (i = 0; i < 2; i++) + { + for (j = 0; j < 4; j++) + l_1329[i][j] = &l_1272; + } + if ((g_739[5] || (((((((*l_1225) >= (~0x6CL)) , (0x48D5L ^ (((((*l_1225) != 0x13D4L) , (void*)0) == (*l_1179)) && (*l_1225)))) > 1L) <= l_1279[2]) , 0x53A9BAF2L) < 0UL))) + { /* block id: 789 */ + int32_t l_1280 = 0x5CD13976L; + int8_t *l_1285 = &l_1276.f3; + uint8_t *l_1286 = (void*)0; + uint8_t *l_1287 = &g_203[3]; + struct S1 l_1288 = {0xDEL,11}; + uint16_t l_1299 = 0xE6BCL; + l_1288 = func_19(l_1280, ((**l_1190) = l_1281), ((*l_1273) ^= 0xD9L), ((*l_1287) |= ((g_994.f1 & (((l_1280 > (4UL >= g_621[1][0][1])) ^ ((*l_1285) ^= (g_745 = l_1284))) <= g_397[7][7])) > 1UL))); + if (l_1187.f2) + { /* block id: 796 */ + const uint8_t *l_1290 = &l_1276.f2; + const uint8_t ** const l_1289[4] = {&l_1290,&l_1290,&l_1290,&l_1290}; + const uint8_t **l_1292 = (void*)0; + const uint8_t ***l_1291 = &l_1292; + struct S1 *l_1293[7]; + int i; + for (i = 0; i < 7; i++) + l_1293[i] = &g_994.f6; + (*l_1291) = l_1289[1]; + l_1294 = l_1192; + return l_1281.f2; + } + else + { /* block id: 800 */ + struct S0 l_1300 = {1603,306,9}; + const struct S1 *l_1320 = &g_70.f6; + const struct S1 **l_1319 = &l_1320; + const struct S1 ***l_1318 = &l_1319; + const struct S1 ****l_1317 = &l_1318; + uint32_t * const l_1326 = &g_217[1]; + uint32_t * const *l_1325 = &l_1326; + (*l_1225) = (-6L); + (*l_1225) = l_1281.f2; + if (l_1301[1]) + break; + l_1300.f0 &= (((l_1299 ^ ((safe_add_func_int16_t_s_s((g_864[8][4][0] | g_1191), (l_1294.f1 , ((safe_lshift_func_uint8_t_u_u(l_1294.f0, 2)) == (&g_217[1] == l_1221[2][6]))))) , ((--(*l_1287)) | (func_30((safe_mul_func_uint8_t_u_u(0x1FL, ((safe_lshift_func_uint8_t_u_u((safe_div_func_int8_t_s_s((((((((l_1314 , l_1115) < 0xE394C0B6L) , l_1315) != (void*)0) , l_1317) == g_1321[2]) || l_1323[5]), 246UL)), 7)) | l_1240))), l_1324) , l_1294.f0)))) , l_1325) != l_1327); + } + } + else + { /* block id: 808 */ + (*l_1225) = l_1324.f0; + l_1329[0][1] = &l_1233; + } + } + l_1349[1][1][4] = func_19((safe_lshift_func_uint8_t_u_u(((((void*)0 != &l_1201) != (((0xA1DDCCE8L < (*l_1225)) , l_754[3]) , (*l_1225))) <= 0x8222L), (*l_1225))), (*l_1126), l_1348, (*l_1225)); + } + } + else + { /* block id: 818 */ + union U3 *l_1350 = &l_1276; + union U3 **l_1351[2]; + uint32_t l_1353 = 4UL; + int32_t l_1366 = 0x100FF667L; + const uint16_t *l_1388[2][7][5] = {{{&l_9,(void*)0,&g_994.f3,&g_255,&g_994.f3},{(void*)0,(void*)0,&g_873[1].f3,(void*)0,(void*)0},{&g_994.f3,&g_994.f3,(void*)0,&l_9,(void*)0},{&g_255,&g_621[0][1][1],&l_9,(void*)0,&g_994.f3},{&g_70.f3,&g_873[1].f3,&g_255,&g_70.f3,(void*)0},{&g_873[1].f3,&g_621[0][1][1],&g_873[1].f3,&g_255,&g_994.f3},{&g_873[1].f3,&g_994.f3,(void*)0,&g_255,&g_70.f3}},{{(void*)0,(void*)0,&g_873[1].f3,&g_70.f3,&l_9},{&g_621[6][1][5],(void*)0,&g_621[0][1][1],(void*)0,&g_621[6][1][5]},{(void*)0,&g_70.f3,&l_9,&l_9,&g_255},{&g_873[1].f3,(void*)0,&l_9,(void*)0,(void*)0},{&g_873[1].f3,(void*)0,&g_621[0][1][1],&g_255,&g_70.f3},{&g_70.f3,(void*)0,&g_873[1].f3,&g_873[1].f3,(void*)0},{&g_255,&g_873[1].f3,(void*)0,(void*)0,&g_255}}}; + uint32_t *l_1397 = &g_217[1]; + uint32_t **l_1396 = &l_1397; + union U3 l_1400[4] = {{0x80L},{0x80L},{0x80L},{0x80L}}; + uint8_t l_1414 = 0x11L; + uint32_t l_1432 = 4294967294UL; + struct S2 *l_1438 = &g_1439; + int32_t l_1444 = 0xD7977A03L; + int32_t l_1445 = 1L; + int32_t l_1446 = 1L; + int32_t l_1447 = 0xA62D7EDBL; + int32_t l_1454 = 0x06D6FB74L; + int8_t l_1455 = (-6L); + uint32_t l_1456 = 0xE877AD48L; + uint16_t l_1467 = 2UL; + uint8_t *l_1494 = (void*)0; + uint8_t *l_1495 = &g_397[6][7]; + union U3 l_1498 = {0xC6L}; + int16_t l_1499 = 0xE9BFL; + struct S1 l_1533[6][8][5] = {{{{0UL,13},{255UL,320},{0x52L,217},{0UL,173},{0xC3L,289}},{{1UL,61},{247UL,80},{0UL,219},{0xC3L,289},{0UL,173}},{{248UL,194},{255UL,320},{0x87L,37},{0UL,219},{0x52L,217}},{{1UL,332},{0UL,18},{247UL,80},{1UL,61},{247UL,80}},{{1UL,61},{0xC3L,289},{0x24L,89},{0x40L,287},{0x85L,59}},{{0x23L,289},{0UL,13},{0xC0L,7},{0x40L,287},{0xBDL,201}},{{0x31L,83},{252UL,64},{0xBDL,201},{1UL,61},{0xC3L,289}},{{0UL,173},{0x40L,287},{255UL,156},{0UL,219},{0x23L,289}}},{{{0x85L,59},{1UL,332},{0xEEL,78},{0xC3L,289},{0x3DL,296}},{{1UL,274},{252UL,64},{247UL,80},{0UL,173},{0x40L,287}},{{0x85L,59},{0UL,219},{0UL,128},{0x52L,217},{248UL,194}},{{0UL,173},{0x31L,83},{0x23L,289},{247UL,80},{0x3DL,296}},{{0x31L,83},{0UL,18},{0x52L,217},{0x85L,59},{0UL,219}},{{0x23L,289},{0x52L,217},{0x52L,217},{0xBDL,201},{0x88L,333}},{{1UL,61},{1UL,274},{0x23L,289},{0xC3L,289},{0xBDL,201}},{{1UL,332},{255UL,320},{0UL,128},{0x23L,289},{0x52L,217}}},{{{248UL,194},{0xBDL,201},{247UL,80},{0x3DL,296},{0UL,31}},{{1UL,61},{0x31L,83},{0xEEL,78},{0x40L,287},{0x52L,217}},{{0UL,13},{0UL,13},{255UL,156},{248UL,194},{0xBDL,201}},{{0x88L,333},{0x3DL,296},{0xBDL,201},{0x3DL,296},{0x88L,333}},{{0UL,173},{1UL,332},{0xC0L,7},{0UL,219},{0UL,219}},{{255UL,320},{1UL,332},{0x24L,89},{0x88L,333},{0x3DL,296}},{{0UL,31},{0x3DL,296},{247UL,80},{0xBDL,201},{248UL,194}},{{0x85L,59},{0UL,13},{0x87L,37},{0x52L,217},{0x40L,287}}},{{{0UL,18},{0x31L,83},{0UL,219},{0UL,31},{0x3DL,296}},{{0x88L,333},{0xBDL,201},{0x52L,217},{0x52L,217},{0x23L,289}},{{0x23L,289},{255UL,320},{0x85L,59},{0xBDL,201},{0xC3L,289}},{{252UL,64},{1UL,274},{0UL,219},{0x88L,333},{0xBDL,201}},{{248UL,194},{0x52L,217},{0UL,128},{0UL,219},{0x85L,59}},{{248UL,194},{0UL,18},{0UL,31},{0x3DL,296},{247UL,80}},{{252UL,64},{0x31L,83},{0x24L,89},{248UL,194},{0x52L,217}},{{0x23L,289},{0UL,219},{255UL,156},{0x40L,287},{0UL,173}}},{{{0x88L,333},{252UL,64},{0UL,173},{0x3DL,296},{0x23L,289}},{{0UL,18},{1UL,332},{255UL,156},{0x23L,289},{248UL,194}},{{0x85L,59},{0x40L,287},{0x24L,89},{0xC3L,289},{0xFAL,126}},{{0UL,31},{252UL,64},{0UL,31},{0xBDL,201},{0UL,173}},{{255UL,320},{0UL,13},{0UL,128},{0x85L,59},{0UL,173}},{{0UL,173},{0xC3L,289},{0UL,219},{247UL,80},{0xFAL,126}},{{0x88L,333},{0UL,18},{0x85L,59},{0x52L,217},{248UL,194}},{{0UL,13},{255UL,320},{0x52L,217},{0UL,173},{0x23L,289}}},{{{1UL,61},{247UL,80},{0UL,219},{0xC3L,289},{0x43L,140}},{{248UL,194},{255UL,320},{0x87L,37},{0UL,219},{0xEEL,78}},{{1UL,332},{0UL,18},{247UL,80},{1UL,61},{0x87L,37}},{{1UL,61},{0xC3L,289},{0x24L,89},{0x40L,287},{255UL,125}},{{0x23L,289},{0UL,13},{252UL,64},{0x40L,287},{9UL,327}},{{0x31L,83},{252UL,64},{9UL,327},{1UL,61},{0x23L,289}},{{0UL,173},{0x40L,287},{1UL,61},{0UL,219},{1UL,332}},{{0x85L,59},{1UL,332},{5UL,232},{0xC3L,289},{1UL,212}}}}; + struct S0 l_1644 = {-1878,566,19}; + int16_t ***l_1649 = (void*)0; + struct S1 *l_1713[4] = {(void*)0,(void*)0,(void*)0,(void*)0}; + uint32_t l_1743 = 4294967294UL; + int i, j, k; + for (i = 0; i < 2; i++) + l_1351[i] = &l_1350; + l_1352 = l_1350; +lbl_1543: + if ((l_1353 = ((*l_1225) | (*l_1225)))) + { /* block id: 821 */ + uint16_t l_1357 = 0x8178L; + union U3 *l_1358 = &l_1347; + const uint32_t l_1363[2][8][10] = {{{0x05B69A1BL,0UL,4294967288UL,1UL,0xB0DB8155L,4294967288UL,0xB0DB8155L,1UL,0UL,0x05B69A1BL},{1UL,0x05B69A1BL,0UL,0xB0DB8155L,4294967288UL,0UL,0UL,0xB0DB8155L,0x05B69A1BL,1UL},{0xB0DB8155L,0xB0DB8155L,0x05B69A1BL,4294967288UL,0UL,0x05B69A1BL,0x05B69A1BL,4294967288UL,1UL,0xB0DB8155L},{4294967288UL,4294967288UL,1UL,0UL,0x05B69A1BL,1UL,1UL,0UL,0xB0DB8155L,4294967288UL},{0UL,0UL,0xB0DB8155L,0x05B69A1BL,1UL,0xB0DB8155L,0xB0DB8155L,0x05B69A1BL,4294967288UL,0UL},{0x05B69A1BL,0x05B69A1BL,4294967288UL,1UL,4294967288UL,4294967288UL,4294967288UL,1UL,0UL,1UL},{1UL,1UL,0UL,0xB0DB8155L,0UL,0UL,0UL,0xB0DB8155L,0x05B69A1BL,0xB0DB8155L},{0xB0DB8155L,0xB0DB8155L,0x05B69A1BL,4294967288UL,0x05B69A1BL,0x05B69A1BL,0x05B69A1BL,4294967288UL,1UL,4294967288UL}},{{4294967288UL,4294967288UL,0xB0DB8155L,0UL,1UL,1UL,1UL,0x05B69A1BL,0xB0DB8155L,0UL},{0UL,0UL,4294967288UL,0x05B69A1BL,0xB0DB8155L,0xB0DB8155L,0xB0DB8155L,1UL,4294967288UL,0x05B69A1BL},{0x05B69A1BL,0x05B69A1BL,0UL,1UL,4294967288UL,4294967288UL,4294967288UL,0xB0DB8155L,0UL,1UL},{0xB0DB8155L,1UL,0x05B69A1BL,0xB0DB8155L,0UL,0x05B69A1BL,0UL,4294967288UL,0x05B69A1BL,0xB0DB8155L},{4294967288UL,0xB0DB8155L,1UL,4294967288UL,0x05B69A1BL,1UL,0x05B69A1BL,0UL,1UL,4294967288UL},{0UL,4294967288UL,0xB0DB8155L,0UL,1UL,0xB0DB8155L,1UL,0x05B69A1BL,4294967288UL,0UL},{0x05B69A1BL,0UL,4294967288UL,1UL,0xB0DB8155L,4294967288UL,0xB0DB8155L,1UL,0UL,0x05B69A1BL},{1UL,0x05B69A1BL,0UL,0xB0DB8155L,4294967288UL,0UL,4294967288UL,0xB0DB8155L,0x05B69A1BL,1UL}}}; + uint16_t *l_1364[10][8][3] = {{{&g_994.f3,&g_255,&l_9},{&l_9,&l_9,&l_1357},{&g_994.f3,&g_994.f3,&g_255},{&g_79,(void*)0,&g_255},{(void*)0,&g_621[2][1][5],(void*)0},{&g_79,(void*)0,&g_621[4][0][2]},{&g_994.f3,&g_994.f3,(void*)0},{(void*)0,(void*)0,&g_255}},{{(void*)0,&g_994.f3,&g_79},{&g_79,&g_79,&g_621[0][1][1]},{&g_70.f3,&l_9,&l_9},{&l_9,(void*)0,(void*)0},{&g_621[0][1][1],&g_621[0][1][1],&g_70.f3},{(void*)0,&g_79,&g_235},{&g_994.f3,&g_621[0][1][1],&l_1357},{&g_79,&g_70.f3,(void*)0}},{{&g_70.f3,(void*)0,&g_621[0][1][1]},{&g_621[0][1][1],(void*)0,&g_70.f3},{(void*)0,&g_621[2][1][5],&g_70.f3},{&g_621[0][1][1],&l_1357,&g_235},{&l_9,&g_994.f3,&g_994.f3},{(void*)0,(void*)0,(void*)0},{(void*)0,&l_1357,&g_994.f3},{&g_70.f3,(void*)0,&g_994.f3}},{{&g_70.f3,&g_235,(void*)0},{&g_255,&g_255,&g_70.f3},{(void*)0,&g_994.f3,&l_9},{&l_9,(void*)0,&l_9},{&l_9,&g_255,&g_621[0][1][1]},{&l_1357,&g_621[0][1][1],&l_9},{&g_255,&l_1357,&g_255},{&g_255,&g_621[4][1][0],(void*)0}},{{(void*)0,(void*)0,&g_255},{&g_621[4][0][2],&l_1357,&l_9},{(void*)0,&g_255,&g_621[0][1][1]},{&g_255,&g_621[0][1][1],&g_79},{&g_79,(void*)0,&g_255},{&g_621[0][1][1],&g_79,&g_994.f3},{&l_9,&g_621[0][1][1],(void*)0},{(void*)0,(void*)0,&g_70.f3}},{{&g_70.f3,&g_235,(void*)0},{&g_235,(void*)0,&l_1357},{&l_1357,&g_70.f3,&g_235},{(void*)0,&l_9,(void*)0},{&g_621[0][1][1],&g_994.f3,(void*)0},{&g_70.f3,(void*)0,&l_9},{&g_70.f3,&g_994.f3,&g_621[2][1][5]},{&g_235,&l_9,&g_79}},{{&g_994.f3,&g_994.f3,(void*)0},{(void*)0,&g_235,&g_79},{&g_994.f3,&g_994.f3,&g_255},{&g_994.f3,&g_70.f3,(void*)0},{(void*)0,(void*)0,&g_255},{&g_70.f3,&g_621[4][1][0],&l_9},{&l_9,&g_621[0][1][1],(void*)0},{&l_9,&g_70.f3,&g_235}},{{&g_255,&g_994.f3,&l_9},{&l_9,&l_9,(void*)0},{&g_255,(void*)0,&g_255},{(void*)0,(void*)0,&g_994.f3},{&g_255,(void*)0,&g_994.f3},{&l_9,&g_235,&l_9},{&g_621[0][1][1],(void*)0,&g_994.f3},{&g_79,&g_235,&g_235}},{{&g_255,&g_994.f3,&l_1357},{&g_994.f3,&g_621[4][0][2],&g_70.f3},{(void*)0,(void*)0,&g_994.f3},{&g_70.f3,&l_9,(void*)0},{&g_255,&g_621[0][1][1],&l_1357},{&l_1357,&g_79,(void*)0},{&g_235,&g_235,&l_9},{(void*)0,(void*)0,(void*)0}},{{(void*)0,&g_621[0][1][1],&g_994.f3},{&l_9,&g_79,(void*)0},{&g_70.f3,&g_235,&g_79},{&g_79,(void*)0,(void*)0},{(void*)0,&g_255,&g_994.f3},{&g_79,&g_79,&g_621[4][0][2]},{&g_255,&g_255,&l_9},{(void*)0,&g_621[4][1][0],(void*)0}}}; + int32_t l_1365 = 0x47BABE44L; + int i, j, k; + l_1366 = (safe_rshift_func_uint16_t_u_u((l_1365 |= ((safe_unary_minus_func_uint8_t_u((l_1357 < ((safe_mod_func_int32_t_s_s((safe_sub_func_int16_t_s_s(((l_1357 == (l_1353 > 0xE5L)) & g_994.f7.f0), 0xF43BL)), l_1363[0][0][1])) , 9UL)))) <= 7L)), 8)); + } + else + { /* block id: 825 */ + int32_t *l_1367 = &g_285[6]; + int32_t *l_1368[6] = {(void*)0,&g_8,&g_285[0],&g_285[0],(void*)0,&g_8}; + int16_t l_1370 = 1L; + uint16_t l_1461 = 65526UL; + union U3 *l_1486 = &l_754[2]; + int i; + g_1371++; + l_1366 = (safe_mul_func_uint8_t_u_u(g_70.f7.f2, g_304.f3)); + for (l_1276.f2 = 0; (l_1276.f2 <= 8); l_1276.f2 += 1) + { /* block id: 830 */ + uint8_t l_1378[5] = {0x4DL,0x4DL,0x4DL,0x4DL,0x4DL}; + union U3 l_1395[6][4][6] = {{{{0x3FL},{1L},{0x00L},{1L},{0x76L},{1L}},{{1L},{0x76L},{0x98L},{1L},{0x3FL},{0x96L}},{{0x3FL},{8L},{0xC5L},{0x98L},{0x3FL},{0x3FL}},{{8L},{0x86L},{0x86L},{0xC5L},{0x3FL},{1L}}},{{{1L},{8L},{0x98L},{0x3FL},{0x86L},{1L}},{{0x76L},{0x96L},{0x3FL},{0x00L},{0x00L},{1L}},{{0x3FL},{0x3FL},{0x98L},{0xC5L},{0x98L},{0L}},{{0x3FL},{0xC5L},{0x76L},{0x96L},{1L},{0x76L}}},{{{0x3FL},{8L},{0L},{0xC5L},{0x00L},{1L}},{{0x86L},{8L},{0x04L},{1L},{0xC5L},{0x04L}},{{0x00L},{8L},{0x98L},{0x76L},{0x00L},{0x76L}},{{0x98L},{0x04L},{0x98L},{0L},{0L},{8L}}},{{{1L},{0L},{0x98L},{0xC5L},{0x04L},{0x76L}},{{0x96L},{0x76L},{8L},{0xC5L},{1L},{0xC5L}},{{0xC5L},{1L},{1L},{0xC5L},{0x00L},{0x98L}},{{0x00L},{1L},{0x86L},{1L},{0x00L},{0x00L}}},{{{0L},{1L},{1L},{0x86L},{0x00L},{0x76L}},{{0x04L},{0L},{0xC5L},{0x00L},{1L},{0x76L}},{{1L},{0x98L},{0x00L},{8L},{8L},{0x76L}},{{0x00L},{0x00L},{1L},{0x3FL},{1L},{0x96L}}},{{{0x00L},{0x86L},{1L},{0x98L},{0x76L},{1L}},{{0x00L},{0L},{0x96L},{0x86L},{0x98L},{0x04L}},{{1L},{0L},{0x3FL},{0x04L},{0x86L},{0x3FL}},{{0x98L},{0L},{1L},{1L},{8L},{1L}}}}; + int32_t l_1406 = 0x84A3375CL; + int32_t l_1409[2][7][7] = {{{(-1L),0x5E4ADE58L,0xA92151E5L,0x4456E609L,0x4456E609L,0x058BA5DFL,(-8L)},{6L,8L,0L,(-3L),0xA1BC5B01L,0L,0L},{(-8L),0x682FACEEL,0xA92151E5L,0x1002CC41L,(-1L),0xA92151E5L,(-1L)},{0x8DB6EE34L,(-8L),8L,0L,0xFA31E86AL,(-3L),0x9B3A76E7L},{0xA92151E5L,0x682FACEEL,0x5E4ADE58L,0xFE2A663DL,0x636A4255L,0x058BA5DFL,(-1L)},{0L,0x710534E2L,0L,6L,0L,0x710534E2L,0x710534E2L},{0xA92151E5L,(-1L),0L,0x058BA5DFL,(-1L),0x1002CC41L,0x682FACEEL}},{{8L,1L,0x8DB6EE34L,0xA1BC5B01L,(-8L),0L,0x710534E2L},{0x5E4ADE58L,0x62B1F322L,0x1002CC41L,0xFE2A663DL,0x62B1F322L,0x4456E609L,0x636A4255L},{0L,8L,0x8DB6EE34L,8L,0xFA31E86AL,0L,0x4E0C0582L},{0L,(-1L),0x682FACEEL,0x682FACEEL,(-8L),(-1L),(-8L)},{0x8DB6EE34L,8L,(-3L),6L,0xFA31E86AL,0xA1BC5B01L,6L},{0x1002CC41L,0x1002CC41L,0xFE2A663DL,0x62B1F322L,0x4456E609L,0xA92151E5L,(-1L)},{0x8DB6EE34L,0xFA31E86AL,0x8DB6EE34L,(-3L),6L,0x9B3A76E7L,(-3L)}}}; + int16_t l_1411 = 1L; + int32_t **l_1435 = &l_1368[1]; + int32_t *l_1440 = &l_1406; + int16_t l_1460 = (-9L); + int32_t l_1474 = 3L; + int i, j, k; + } + } + if (((*l_1225) && 0x7FFFL)) + { /* block id: 894 */ + const union U3 ***l_1496 = &g_663[0][0][3]; + const int32_t l_1497 = (-2L); + int32_t l_1504 = 1L; + uint8_t l_1505 = 255UL; + struct S0 l_1555[4] = {{85,152,2},{85,152,2},{85,152,2},{85,152,2}}; + struct S1 *l_1577 = &g_361; + int16_t *l_1584 = &g_1191; + int16_t *l_1597 = &g_252; + int16_t *l_1598[7]; + int i; + for (i = 0; i < 7; i++) + l_1598[i] = &l_1499; + (*l_1496) = &g_664; + (*l_1225) |= (0xE8E8L != (6L == ((void*)0 != &g_1183))); + if (((l_1497 , l_1497) , ((l_1497 < l_1499) , (*l_1225)))) + { /* block id: 897 */ + int16_t l_1500 = 0x9452L; + int32_t *l_1501 = &g_8; + int32_t *l_1502 = (void*)0; + int32_t *l_1503[10] = {&g_8,&l_1444,&l_1452[0],&l_1366,&l_1444,&l_1444,&l_1452[0],&g_8,&l_1445,&l_1452[0]}; + int8_t l_1515 = 0x77L; + uint32_t l_1527[2][1]; + struct S1 l_1534 = {1UL,311}; + struct S2 **l_1548 = &g_1269; + union U3 l_1551 = {0x44L}; + int i, j; + for (i = 0; i < 2; i++) + { + for (j = 0; j < 1; j++) + l_1527[i][j] = 1UL; + } + l_1505++; + for (l_1498.f3 = 9; (l_1498.f3 != 23); ++l_1498.f3) + { /* block id: 901 */ + uint16_t *l_1510 = &g_994.f3; + uint32_t *l_1516 = (void*)0; + uint32_t *l_1517 = &g_829[0]; + int16_t ***l_1526 = &g_1524; + int32_t l_1532 = 0x6526D43DL; + (*l_1225) = (((((*l_1517) = (((l_1510 != &l_9) != l_1505) || (*l_1225))) ^ (*l_1501)) >= (*l_1501)) > l_1447); + (*l_1501) ^= 0xD0C593A4L; + l_1534 = ((g_351 > (safe_mod_func_uint8_t_u_u(((((*l_360) = g_60.f0) == g_70.f8) && ((*g_1525) &= (safe_sub_func_uint32_t_u_u(l_1505, (safe_sub_func_uint16_t_u_u((((*l_1526) = g_1524) == (g_1529 = (l_1527[1][0] , g_1528))), (safe_div_func_int32_t_s_s(l_1455, l_1532)))))))), 5L))) , l_1533[3][4][3]); + return g_873[1].f8; + } + for (g_489.f2 = 0; (g_489.f2 > 35); g_489.f2 = safe_add_func_int16_t_s_s(g_489.f2, 1)) + { /* block id: 915 */ + uint8_t l_1540 = 1UL; + int32_t *l_1557 = &l_1452[0]; + struct S2 **l_1558 = &g_1269; + uint16_t l_1561[5][8] = {{0xD1F5L,65535UL,1UL,0xC146L,0xD1F5L,65535UL,65528UL,8UL},{0xC146L,0xC146L,1UL,65535UL,1UL,1UL,8UL,65528UL},{1UL,1UL,1UL,0xD1F5L,1UL,1UL,1UL,8UL},{0xD1F5L,1UL,8UL,65535UL,1UL,1UL,65528UL,1UL},{1UL,0xC146L,0xC146L,65528UL,1UL,1UL,0xC146L,65528UL}}; + union U3 l_1573 = {0xCFL}; + struct S1 **l_1576[7][9][3] = {{{&g_156[2][2][2],&g_156[2][3][6],&g_156[2][1][7]},{&g_156[2][1][7],&g_156[2][7][2],(void*)0},{&g_156[2][1][7],&g_156[2][7][2],&g_156[2][1][7]},{&g_156[2][3][6],&g_156[0][2][7],&g_156[2][1][7]},{&g_156[2][1][7],&g_156[2][1][7],(void*)0},{&g_156[2][1][7],&g_156[2][1][7],&g_156[2][1][7]},{&g_156[2][1][7],&g_156[2][1][7],&g_156[2][1][6]},{&g_156[2][1][7],&g_156[2][1][7],&g_156[0][5][3]},{&g_156[2][1][7],&g_156[3][2][3],&g_156[2][1][7]}},{{&g_156[0][1][7],&g_156[2][4][0],(void*)0},{&g_156[2][1][7],&g_156[2][2][2],&g_156[2][7][2]},{&g_156[2][1][7],&g_156[0][5][3],&g_156[2][2][2]},{&g_156[2][4][0],&g_156[2][7][2],&g_156[3][2][3]},{&g_156[2][7][2],&g_156[0][5][3],&g_156[2][1][7]},{&g_156[2][1][7],&g_156[2][1][7],&g_156[2][7][2]},{&g_156[2][1][7],&g_156[2][7][2],&g_156[2][1][7]},{&g_156[2][1][7],&g_156[2][1][7],&g_156[2][1][7]},{&g_156[2][1][7],&g_156[0][1][7],(void*)0}},{{&g_156[2][1][6],&g_156[2][1][7],&g_156[0][3][0]},{&g_156[2][1][7],(void*)0,&g_156[2][2][2]},{&g_156[0][3][0],&g_156[2][1][7],(void*)0},{&g_156[2][1][7],&g_156[3][2][3],&g_156[0][1][7]},{&g_156[0][5][3],&g_156[2][1][7],&g_156[2][7][2]},{&g_156[2][1][7],&g_156[2][1][7],&g_156[0][5][3]},{(void*)0,&g_156[0][5][3],&g_156[2][1][7]},{&g_156[0][3][0],&g_156[0][3][0],(void*)0},{(void*)0,&g_156[2][1][6],&g_156[2][7][2]}},{{&g_156[2][1][7],&g_156[2][1][7],&g_156[2][1][7]},{&g_156[2][1][7],&g_156[2][1][7],(void*)0},{&g_156[2][1][7],&g_156[0][2][7],&g_156[2][1][7]},{&g_156[2][1][7],&g_156[0][1][7],&g_156[2][1][6]},{&g_156[2][1][7],(void*)0,&g_156[0][5][3]},{&g_156[0][5][3],&g_156[2][1][7],&g_156[2][1][7]},{&g_156[2][1][7],&g_156[2][1][7],(void*)0},{&g_156[2][1][7],&g_156[2][1][7],&g_156[2][7][2]},{&g_156[0][2][7],&g_156[2][1][7],&g_156[2][2][2]}},{{&g_156[2][1][7],&g_156[0][3][0],&g_156[3][2][3]},{&g_156[1][4][6],&g_156[2][1][7],&g_156[2][1][7]},{&g_156[3][2][3],&g_156[2][7][2],&g_156[2][7][2]},{&g_156[0][3][0],&g_156[2][1][6],&g_156[2][1][7]},{&g_156[0][2][7],&g_156[2][1][7],&g_156[2][1][7]},{&g_156[2][1][7],&g_156[2][1][7],(void*)0},{&g_156[2][7][2],(void*)0,&g_156[0][3][0]},{&g_156[3][2][3],&g_156[3][2][3],&g_156[2][2][2]},{&g_156[2][7][2],&g_156[2][1][7],(void*)0}},{{(void*)0,&g_156[2][1][7],&g_156[0][1][7]},{&g_156[2][3][6],&g_156[2][1][7],&g_156[2][7][2]},{&g_156[0][1][7],&g_156[2][2][2],&g_156[0][5][3]},{&g_156[2][1][7],&g_156[2][1][7],&g_156[2][1][7]},{&g_156[2][7][2],&g_156[2][1][7],(void*)0},{(void*)0,&g_156[1][4][6],&g_156[2][7][2]},{&g_156[0][5][3],&g_156[2][1][7],&g_156[2][1][7]},{&g_156[0][1][7],&g_156[0][1][7],(void*)0},{&g_156[2][2][2],&g_156[2][1][7],&g_156[2][1][7]}},{{&g_156[0][2][7],&g_156[2][1][7],&g_156[2][1][6]},{&g_156[2][4][0],(void*)0,&g_156[0][5][3]},{&g_156[2][3][6],(void*)0,&g_156[2][1][7]},{&g_156[2][1][7],&g_156[2][1][7],(void*)0},{&g_156[2][2][2],&g_156[2][1][7],&g_156[2][7][2]},{&g_156[2][1][7],&g_156[2][3][6],&g_156[2][2][2]},{&g_156[2][1][7],&g_156[2][1][7],&g_156[3][2][3]},{&g_156[2][1][6],&g_156[2][3][6],&g_156[2][1][7]},{(void*)0,&g_156[0][3][0],&g_156[2][7][2]}}}; + int i, j, k; + for (g_304.f3 = 18; (g_304.f3 != 4); g_304.f3 = safe_sub_func_uint32_t_u_u(g_304.f3, 1)) + { /* block id: 918 */ + int16_t l_1539 = 0x0075L; + int16_t ***l_1544 = (void*)0; + int16_t ***l_1545 = &g_1524; + l_1540++; + if (g_994.f7.f0) + goto lbl_1543; + (*l_1545) = &g_1525; + (*l_1225) = g_70.f6.f0; + } + for (l_1366 = 12; (l_1366 != (-7)); l_1366--) + { /* block id: 926 */ + (*l_1225) ^= (l_1548 != (void*)0); + if (l_1497) + continue; + (*l_1501) &= (l_1504 &= (*l_1225)); + (*g_124) = &g_285[0]; + } + if (((*l_1501) = ((safe_div_func_int16_t_s_s(func_24((l_1551 , (safe_unary_minus_func_int32_t_s((l_1504 && (l_1504 != (func_24((safe_add_func_uint32_t_u_u(l_1497, (*l_1225))), (**g_112), (*g_664), g_994.f0.f2, (*g_664)) > 0xBCL)))))), l_1555[0], (***l_1496), l_1432, l_1556), (-9L))) != 255UL))) + { /* block id: 934 */ + struct S2 ***l_1559[6][8][5] = {{{&l_1558,&l_1548,&l_1558,&l_1558,(void*)0},{&l_1558,&l_1548,&l_1558,&l_1558,&l_1548},{&l_1558,&l_1558,&l_1548,&l_1548,&l_1558},{(void*)0,&l_1548,&l_1548,&l_1558,&l_1548},{&l_1558,&l_1548,&l_1548,&l_1548,&l_1558},{&l_1558,&l_1558,(void*)0,&l_1558,&l_1548},{&l_1548,&l_1558,&l_1558,(void*)0,&l_1548},{(void*)0,&l_1548,(void*)0,&l_1548,(void*)0}},{{(void*)0,&l_1558,&l_1558,&l_1548,&l_1558},{&l_1548,(void*)0,(void*)0,&l_1558,&l_1558},{&l_1558,&l_1548,&l_1548,&l_1548,&l_1548},{&l_1558,&l_1548,&l_1558,&l_1558,&l_1558},{&l_1558,&l_1548,&l_1548,&l_1548,&l_1548},{&l_1548,&l_1548,&l_1548,&l_1558,&l_1548},{&l_1558,&l_1558,&l_1548,(void*)0,&l_1548},{&l_1558,&l_1558,(void*)0,&l_1548,&l_1558}},{{(void*)0,&l_1558,&l_1548,&l_1548,(void*)0},{&l_1548,&l_1558,&l_1558,&l_1558,&l_1548},{&l_1558,&l_1558,&l_1558,&l_1548,&l_1558},{&l_1558,&l_1558,&l_1548,&l_1558,(void*)0},{&l_1548,&l_1548,(void*)0,&l_1548,&l_1548},{&l_1548,&l_1558,&l_1548,&l_1558,&l_1558},{&l_1548,(void*)0,&l_1558,&l_1558,&l_1548},{&l_1548,&l_1558,&l_1558,&l_1548,&l_1558}},{{(void*)0,&l_1558,&l_1558,&l_1548,(void*)0},{&l_1548,&l_1558,&l_1548,&l_1558,(void*)0},{&l_1548,&l_1548,&l_1548,&l_1548,&l_1548},{(void*)0,&l_1558,(void*)0,&l_1558,&l_1548},{&l_1558,&l_1548,(void*)0,&l_1548,&l_1548},{&l_1558,(void*)0,&l_1558,&l_1548,&l_1548},{&l_1548,(void*)0,&l_1558,&l_1558,&l_1548},{&l_1558,&l_1558,&l_1548,&l_1548,&l_1548}},{{&l_1558,&l_1548,&l_1558,&l_1548,&l_1558},{&l_1558,(void*)0,&l_1558,&l_1558,&l_1558},{&l_1558,&l_1548,&l_1548,&l_1548,&l_1558},{(void*)0,&l_1548,&l_1548,&l_1558,&l_1558},{&l_1548,&l_1548,&l_1558,&l_1558,&l_1548},{&l_1558,&l_1548,&l_1548,&l_1548,&l_1558},{&l_1548,&l_1558,&l_1558,&l_1558,(void*)0},{&l_1558,(void*)0,&l_1558,&l_1548,&l_1548}},{{&l_1558,&l_1558,&l_1558,&l_1548,&l_1558},{(void*)0,(void*)0,&l_1548,&l_1558,&l_1548},{&l_1558,&l_1548,&l_1548,&l_1548,&l_1558},{(void*)0,&l_1558,&l_1558,&l_1558,(void*)0},{&l_1548,(void*)0,(void*)0,&l_1558,&l_1548},{&l_1548,&l_1548,&l_1558,&l_1548,(void*)0},{&l_1548,&l_1548,&l_1558,&l_1558,&l_1558},{&l_1558,&l_1548,&l_1548,&l_1548,&l_1558}}}; + int i, j, k; + (*g_124) = l_1557; + if ((*l_1225)) + break; + l_1548 = l_1558; + } + else + { /* block id: 938 */ + uint16_t l_1560 = 1UL; + int32_t l_1572 = 0x359A7594L; + uint16_t *l_1575 = &g_994.f3; + l_1561[1][4] = (l_1560 < g_1439.f2.f2); + (*l_1501) &= ((&g_829[1] == ((safe_sub_func_int32_t_s_s((((l_1192 , 0UL) > (safe_mod_func_uint16_t_u_u((((*l_360) = (safe_add_func_int32_t_s_s(((g_1439.f3 = ((*l_1575) = ((((safe_rshift_func_int8_t_s_s(func_24((++(**l_1396)), func_30((l_1572 = (l_1400[1].f3 , g_987[0][0][0])), ((*l_1352) = l_1573)), (***l_1496), g_449, l_1573), l_1574)) || l_1572) > (-1L)) ^ 1L))) == l_1498.f2), (*l_1557)))) | l_1555[0].f1), g_70.f7.f1))) <= l_1498.f2), l_1560)) , &g_829[1])) == g_361.f1); + } + l_1577 = &l_1533[4][0][4]; + } + } + else + { /* block id: 950 */ + for (g_79 = (-14); (g_79 < 4); g_79 = safe_add_func_uint16_t_u_u(g_79, 8)) + { /* block id: 953 */ + for (g_489.f2 = 0; (g_489.f2 <= 0); g_489.f2 += 1) + { /* block id: 956 */ + int i, j; + return g_54[(g_489.f2 + 1)][(g_489.f2 + 1)]; + } + } + } + (*g_124) = (((l_1504 = (g_54[9][7] &= (safe_add_func_uint8_t_u_u(((((*l_1495) = (*l_1225)) != (*l_1225)) >= l_1504), ((((*l_1225) ^ (safe_rshift_func_int16_t_s_s((((**g_1524) && ((*l_1584) = ((*g_113) , ((*g_1525) = (*g_1525))))) ^ (safe_lshift_func_int8_t_s_s(((((*l_1597) = (safe_lshift_func_int16_t_s_u((safe_rshift_func_int8_t_s_u((safe_add_func_uint8_t_u_u((safe_rshift_func_uint16_t_u_u(l_1505, (safe_lshift_func_int8_t_s_u((((8UL > g_1439.f0.f1) <= (-1L)) == l_1353), 7)))), (-7L))), 4)), g_254[0]))) == l_1555[0].f2) < l_1505), l_1353))), l_1353))) , l_1498.f0) == (*l_1225)))))) <= l_1505) , (void*)0); + } + else + { /* block id: 968 */ + int32_t *l_1599 = &g_1441; + int32_t *l_1600 = (void*)0; + int32_t l_1601 = 0xAE42B3BBL; + int32_t *l_1602 = &l_1446; + int32_t *l_1603 = &l_1454; + int32_t *l_1604 = &l_1449; + int32_t *l_1605[1][10] = {{&l_1449,&l_1447,(void*)0,&l_1447,(void*)0,&l_1449,&l_1449,&l_1449,&l_1447,&l_1447}}; + uint32_t l_1606[4]; + int i, j; + for (i = 0; i < 4; i++) + l_1606[i] = 0x8DE90388L; + ++l_1606[1]; + for (l_1454 = 28; (l_1454 == (-8)); l_1454 = safe_sub_func_uint16_t_u_u(l_1454, 1)) + { /* block id: 972 */ + struct S0 ***l_1611 = &g_258; + int32_t l_1612 = 0x4D626EE6L; + (*l_1611) = &g_259; + l_1612 &= (*l_1225); + return (*l_1225); + } + } + if ((*l_1225)) + { /* block id: 978 */ + uint32_t l_1621 = 0x24DC288AL; + union U3 l_1645 = {0xB5L}; + int32_t l_1655 = 0xD35CC852L; + int16_t ***l_1656 = &g_1524; + (*l_1225) &= 0xB443BAC4L; + for (l_1498.f0 = (-13); (l_1498.f0 < (-15)); l_1498.f0--) + { /* block id: 982 */ + uint16_t l_1615 = 0x80FAL; + int32_t l_1650[9][3][8] = {{{0x9D1FDFD4L,0x9D1FDFD4L,5L,0x9102FFFDL,1L,1L,0x9102FFFDL,5L},{0x9D1FDFD4L,0x9D1FDFD4L,5L,0x9102FFFDL,1L,1L,0x9102FFFDL,5L},{0x9D1FDFD4L,0x9D1FDFD4L,5L,0x9102FFFDL,1L,1L,0x9102FFFDL,5L}},{{0x9D1FDFD4L,0x9D1FDFD4L,5L,0x9102FFFDL,1L,1L,0x9102FFFDL,5L},{0x9D1FDFD4L,0x9D1FDFD4L,5L,0x9102FFFDL,1L,1L,0x9102FFFDL,5L},{0x9D1FDFD4L,0x9D1FDFD4L,5L,0x9102FFFDL,1L,1L,0x9102FFFDL,5L}},{{0x9D1FDFD4L,0x9D1FDFD4L,5L,0x9102FFFDL,1L,1L,0x9102FFFDL,5L},{0x9D1FDFD4L,0x9D1FDFD4L,5L,0x9102FFFDL,1L,1L,0x9102FFFDL,5L},{0x9D1FDFD4L,0x9D1FDFD4L,5L,0x9102FFFDL,1L,1L,0x9102FFFDL,5L}},{{0x9D1FDFD4L,0x9D1FDFD4L,5L,0x9102FFFDL,1L,1L,0x9102FFFDL,5L},{0x9D1FDFD4L,0x9D1FDFD4L,5L,0x9102FFFDL,1L,1L,0x9102FFFDL,5L},{0x9D1FDFD4L,0x9D1FDFD4L,5L,0x9102FFFDL,1L,1L,0x9102FFFDL,5L}},{{0x9D1FDFD4L,0x9D1FDFD4L,5L,0x9102FFFDL,1L,1L,0x9102FFFDL,5L},{0x9D1FDFD4L,0x9D1FDFD4L,5L,0x9102FFFDL,1L,1L,0x9102FFFDL,5L},{0x9D1FDFD4L,0x9D1FDFD4L,5L,0x9102FFFDL,1L,1L,0x9102FFFDL,5L}},{{0x9D1FDFD4L,0x9D1FDFD4L,5L,0x9102FFFDL,1L,1L,0x9102FFFDL,5L},{0x9D1FDFD4L,0x9D1FDFD4L,5L,0x9102FFFDL,1L,1L,0x9102FFFDL,5L},{0x9D1FDFD4L,0x9D1FDFD4L,5L,0x9102FFFDL,1L,1L,0x9102FFFDL,5L}},{{0x9D1FDFD4L,0x9D1FDFD4L,5L,0x9102FFFDL,1L,1L,0x9102FFFDL,5L},{0x9D1FDFD4L,0x9D1FDFD4L,5L,0x9102FFFDL,1L,1L,0x9102FFFDL,5L},{0x9D1FDFD4L,0x9D1FDFD4L,5L,0x9102FFFDL,1L,1L,0x9102FFFDL,5L}},{{0x9D1FDFD4L,0x9D1FDFD4L,5L,0x9102FFFDL,1L,1L,0x9102FFFDL,5L},{0x9D1FDFD4L,0x9D1FDFD4L,5L,0x9102FFFDL,1L,1L,0x9102FFFDL,5L},{0x9D1FDFD4L,0x9D1FDFD4L,5L,0x9102FFFDL,1L,1L,0x9102FFFDL,5L}},{{0x9D1FDFD4L,0x9D1FDFD4L,5L,0x9102FFFDL,1L,1L,0x9102FFFDL,5L},{0x9D1FDFD4L,0x9D1FDFD4L,5L,0x9102FFFDL,1L,1L,0x9102FFFDL,5L},{0x9D1FDFD4L,0x9D1FDFD4L,5L,0x9102FFFDL,1L,1L,0x9102FFFDL,5L}}}; + struct S0 l_1654[6][8] = {{{388,541,14},{-1172,584,11},{-1559,260,10},{1907,696,16},{388,541,14},{210,414,5},{-221,347,11},{394,258,18}},{{388,541,14},{-1191,582,1},{1907,696,16},{394,258,18},{394,258,18},{-221,347,11},{-1559,260,10},{-221,347,11}},{{-1559,260,10},{-1191,582,1},{-221,347,11},{-1191,582,1},{210,414,5},{-1191,582,1},{394,258,18},{-1559,260,10}},{{-1172,584,11},{-1172,584,11},{-221,347,11},{210,414,5},{388,541,14},{388,541,14},{-1559,260,10},{394,258,18}},{{-221,347,11},{-1962,219,9},{1907,696,16},{-1191,582,1},{1907,696,16},{-221,347,11},{1907,696,16},{-1559,260,10}},{{210,414,5},{388,541,14},{-1559,260,10},{-1172,584,11},{1907,696,16},{-1962,219,9},{-1962,219,9},{-221,347,11}}}; + int i, j, k; + for (l_1574 = 0; (l_1574 <= 7); l_1574 += 1) + { /* block id: 985 */ + uint32_t l_1616 = 0xFD122033L; + if (l_1615) + break; + l_1192 = ((l_1615 > ((*l_1495) = l_1616)) , l_1192); + } + for (l_1347.f0 = 0; (l_1347.f0 <= (-15)); l_1347.f0 = safe_sub_func_int32_t_s_s(l_1347.f0, 4)) + { /* block id: 992 */ + uint16_t l_1633 = 0xA3D7L; + const union U3 l_1646 = {0x27L}; + uint32_t *l_1667 = &g_351; + (*l_1225) = (safe_mul_func_int8_t_s_s(l_1621, 255UL)); + for (g_1371 = (-11); (g_1371 == 25); g_1371++) + { /* block id: 996 */ + int32_t l_1632 = 0x9A959592L; + int32_t *l_1635 = &l_1446; + int32_t *l_1647 = (void*)0; + int32_t *l_1648 = &l_1447; + int16_t *l_1653 = &l_1369[8][0]; + (*l_1635) |= ((*l_1225) &= ((safe_rshift_func_int16_t_s_u(((g_1634 = (((*l_1495) = (((*l_360) = ((func_30(l_1414, (*l_1350)) , (g_873[1].f3 == ((0x08L != ((&g_79 != &l_1467) == (safe_sub_func_uint32_t_u_u((safe_lshift_func_uint16_t_u_u(((safe_div_func_uint16_t_u_u(((l_1366 , l_1632) && l_1632), 0x707FL)) , g_524.f3), 10)), 0x0E96D649L)))) , 0xD5DCL))) <= 65527UL)) < l_1633)) , &g_489)) != (void*)0), 5)) , l_1633)); + (*l_1648) ^= (safe_mod_func_uint8_t_u_u((safe_add_func_int16_t_s_s(((*l_1635) , func_24((safe_mul_func_int16_t_s_s((safe_add_func_int32_t_s_s((3L || l_1444), ((void*)0 == &g_829[1]))), (g_873[1].f0.f1 != (*l_1225)))), l_1644, (l_1645 = (*l_1350)), (*l_1225), l_1646)), (**g_1524))), 249UL)); + (*l_1225) = ((*l_1648) = (((l_1649 = &g_1524) == (func_19((**g_1524), ((((l_1650[5][0][4] = l_1446) , 0xBA97B269L) || (safe_mod_func_int16_t_s_s(l_1646.f2, g_70.f8))) , (*g_664)), l_1655, l_1646.f3) , l_1656)) < 0xB04F3DDBL)); + (*l_1225) &= (safe_div_func_int16_t_s_s(l_1645.f2, l_1655)); + } + for (g_745 = 0; (g_745 <= 1); g_745 += 1) + { /* block id: 1013 */ + int i; + l_1452[g_745] = l_1452[g_745]; + l_1438 = (void*)0; + } + (*l_1225) ^= (l_1366 = ((l_1645.f2 , g_1120[2]) > ((*l_1667) = g_994.f2.f2))); + } + } + } + else + { /* block id: 1022 */ + struct S2 **l_1670[4][9] = {{&l_1438,&l_1438,&g_1269,&g_1269,(void*)0,&g_1269,&l_1438,&l_1438,(void*)0},{&g_1269,(void*)0,(void*)0,&g_1269,&g_1269,&g_1269,&l_1438,&l_1438,&l_1438},{(void*)0,&l_1438,&l_1438,(void*)0,&g_1269,(void*)0,&l_1438,(void*)0,&l_1438},{&g_1269,&g_1269,&g_1269,&l_1438,(void*)0,&l_1438,&g_1269,&l_1438,&l_1438}}; + int32_t l_1687 = 0xC0D3F221L; + int32_t *l_1688 = &l_1451; + union U3 l_1689 = {6L}; + union U3 *l_1696[8][9][1] = {{{&l_1689},{&l_1689},{&g_304},{&l_1400[1]},{&l_1689},{&l_1689},{&l_1276},{&l_1689},{&l_1689}},{{&l_1347},{&l_1689},{&l_1400[1]},{&g_304},{&l_1400[1]},{&l_1276},{&l_1689},{&l_1276},{&l_1347}},{{&g_304},{&l_1347},{&g_304},{&l_1689},{&g_304},{&l_1689},{&l_1689},{&l_1689},{&l_1689}},{{&l_1400[1]},{&l_1689},{&l_1400[1]},{&l_1276},{&l_1400[1]},{&l_1689},{&l_1347},{&l_1276},{&l_1400[1]}},{{&g_304},{&l_1347},{&l_1276},{&l_1689},{&g_304},{&l_1347},{&l_1689},{&l_1347},{&g_304}},{{&l_1400[1]},{&g_304},{&l_1689},{&l_1276},{&l_1689},{&l_1689},{&l_1347},{&l_1689},{&l_1400[1]}},{{&g_304},{&l_1400[1]},{&l_1276},{&l_1347},{&l_1276},{&l_1347},{&g_304},{&l_1347},{&g_304}},{{&l_1689},{&g_304},{&l_1689},{&l_1689},{&l_1689},{&l_1689},{&l_1400[1]},{&l_1689},{&l_1689}}}; + union U3 *l_1697[6][7] = {{&l_1400[1],&l_754[3],&l_1347,&l_1689,&l_1347,&l_1347,(void*)0},{(void*)0,&l_1400[1],(void*)0,&l_1400[1],&l_754[0],&l_1347,&g_304},{&l_1347,&g_304,(void*)0,(void*)0,&l_754[3],&g_489,&l_1347},{&l_1400[1],&l_1400[1],&l_754[0],&l_1347,&l_754[0],&l_1400[1],&l_1400[1]},{&l_754[0],&l_1347,&g_304,&l_1347,&l_1347,(void*)0,&g_304},{&g_489,&g_304,(void*)0,&l_1347,(void*)0,&l_754[0],&l_1400[1]}}; + union U3 *l_1698[2]; + union U3 *l_1699 = &l_754[3]; + uint8_t l_1700 = 0xD5L; + struct S1 *l_1711 = &g_1672.f6; + uint8_t * const *l_1715 = (void*)0; + uint32_t l_1730 = 0xEB5B1C50L; + int32_t l_1736 = 9L; + int32_t *l_1737 = &g_8; + int32_t *l_1738 = &l_1447; + int32_t *l_1739 = (void*)0; + int32_t *l_1740 = &l_1366; + int32_t *l_1741 = &l_1444; + int32_t *l_1742[7]; + int i, j, k; + for (i = 0; i < 2; i++) + l_1698[i] = &l_1400[0]; + for (i = 0; i < 7; i++) + l_1742[i] = &l_1687; + (*l_1225) = (safe_sub_func_int16_t_s_s((((l_1671 = &g_994) == (void*)0) , (safe_div_func_uint16_t_u_u((0xB6A98EE7L & func_24((safe_rshift_func_int16_t_s_u((((*l_1688) = ((-1L) <= ((*l_1225) = ((safe_add_func_int32_t_s_s((0L > ((*l_1225) | (!((*l_360) = (safe_div_func_int32_t_s_s((safe_mod_func_uint32_t_u_u((safe_mul_func_uint16_t_u_u((safe_lshift_func_int16_t_s_s((((((*l_1225) != (*l_1225)) > g_235) , l_1671) == (void*)0), 2)), (*g_1525))), 0xECDF187DL)), l_1687)))))), l_1687)) , (*l_1225))))) | 1L), g_1439.f6.f0)), (**g_112), l_1689, g_1439.f0.f0, l_1689)), g_1120[2]))), 1UL)); + if (((*l_1688) = ((((safe_add_func_uint16_t_u_u((safe_sub_func_int32_t_s_s(7L, (((((*l_1225) = (g_1672.f0.f0 != l_1432)) , &g_873[1]) != &g_873[1]) & (*l_1688)))), g_873[1].f4)) | l_1446) == g_217[1]) > l_1700))) + { /* block id: 1031 */ + int8_t *l_1705[4]; + uint8_t * const * const l_1714[6][7] = {{(void*)0,&g_904,&g_904,(void*)0,(void*)0,&g_904,&g_904},{&l_1494,&g_904,&l_1494,&g_904,&l_1494,&g_904,&l_1494},{(void*)0,(void*)0,&g_904,&g_904,(void*)0,(void*)0,&g_904},{&l_1494,&g_904,&l_1494,&g_904,&l_1494,&g_904,&l_1494},{(void*)0,&g_904,&g_904,(void*)0,(void*)0,&g_904,&g_904},{&l_1494,&g_904,&l_1494,&g_904,&l_1494,&g_904,&l_1494}}; + int32_t l_1731 = 0x43CC042CL; + int i, j; + for (i = 0; i < 4; i++) + l_1705[i] = &g_921; + if (g_255) + { /* block id: 1032 */ + (*g_124) = &l_1687; + } + else + { /* block id: 1034 */ + uint32_t *l_1701 = (void*)0; + uint32_t *l_1702 = &g_829[0]; + uint32_t **l_1706 = &l_1397; + const int32_t ***l_1720 = (void*)0; + const int32_t ***l_1721 = &l_1718[5]; + int32_t l_1732 = 0x93DFC5F6L; + (*l_1225) &= (*l_1688); + if ((((*l_1702)--) == ((void*)0 != l_1705[3]))) + { /* block id: 1037 */ + uint32_t ***l_1707 = &l_1396; + int32_t l_1710[10][6] = {{6L,0x3B16ECF0L,0L,(-8L),0x3B16ECF0L,0x3B16ECF0L},{0x3B16ECF0L,8L,8L,0x0A6BF73EL,0L,0x3B16ECF0L},{0x0F36165FL,(-8L),0L,0L,8L,0L},{0x0F36165FL,6L,0x0F36165FL,0x0A6BF73EL,0x3B16ECF0L,(-8L)},{0x3B16ECF0L,6L,0x0A6BF73EL,8L,8L,0x0F36165FL},{(-8L),(-8L),0x0A6BF73EL,(-8L),6L,(-8L)},{0x0F36165FL,0x0A6BF73EL,0x0F36165FL,(-8L),0L,0L},{(-8L),0L,6L,8L,0L,0L},{0L,0x0A6BF73EL,0x0A6BF73EL,0x0F36165FL,6L,0L},{0x3B16ECF0L,(-8L),6L,6L,0x0A6BF73EL,6L}}; + int i, j; + (*l_1707) = l_1706; + (*l_1688) = (safe_add_func_uint8_t_u_u((*l_1688), l_1710[5][3])); + return l_1400[1].f2; + } + else + { /* block id: 1041 */ + struct S1 **l_1712[8] = {&g_156[2][1][7],&g_156[2][1][7],&g_156[2][1][7],&g_156[2][1][7],&g_156[2][1][7],&g_156[2][1][7],&g_156[2][1][7],&g_156[2][1][7]}; + int i; + l_1713[0] = l_1711; + l_1715 = l_1714[4][4]; + } + (*l_1225) &= ((l_1445 & (safe_sub_func_uint32_t_u_u(g_829[0], (*l_1688)))) ^ ((&l_1688 != ((*l_1721) = l_1718[5])) ^ ((safe_mul_func_int16_t_s_s((safe_lshift_func_uint16_t_u_s(g_994.f0.f1, 2)), ((safe_rshift_func_int8_t_s_u(((l_1732 = (safe_mul_func_int8_t_s_s(0x77L, (((*l_1719) | l_1731) , (*l_1688))))) > (*l_1688)), (*l_1688))) & 0xEAL))) & 0UL))); + for (g_1191 = 0; (g_1191 > 17); g_1191 = safe_add_func_int32_t_s_s(g_1191, 6)) + { /* block id: 1050 */ + uint16_t l_1735 = 65535UL; + if (l_1735) + break; + } + } + } + else + { /* block id: 1054 */ + return g_1439.f6.f1; + } + ++l_1743; + } + } + (*l_1225) = (*l_1719); + return g_1672.f6.f1; +} + + +/* ------------------------------------------ */ +/* + * reads : g_60.f2 g_285 g_70.f3 g_141 g_217 g_60.f0 g_252 g_70.f2.f0 g_304.f2 g_351 g_70.f6.f1 g_524.f2 g_79 g_304.f3 g_8 g_113 g_70.f7 g_449 g_124 g_489.f2 g_664 g_489 g_489.f3 g_67 g_70.f0.f0 g_361.f1 g_489.f0 g_873.f2.f2 g_739 g_70.f0.f2 g_903 g_258 g_259 g_70.f2 g_235 g_873.f6.f0 g_70.f6.f0 g_921 g_281 g_873.f8 g_873.f4 g_54 g_70 g_112 g_873.f0.f0 g_873.f2.f1 g_125 g_973 g_987 g_829 g_994.f4 g_994.f2.f2 g_994.f8 g_254 g_873.f5 g_747 g_873.f7.f0 g_994.f0.f2 g_397 g_60.f1 g_994.f2.f0 g_745 g_255 + * writes: g_285 g_252 g_141 g_70.f3 g_351 g_70.f2.f0 g_125 g_489.f2 g_489.f3 g_361.f1 g_254 g_235 g_67 g_79 g_113 g_962 g_489 g_397 g_994.f2.f0 g_217 g_70.f6 + */ +static uint16_t func_2(int32_t p_3, uint8_t p_4, int16_t p_5, union U3 p_6, const uint32_t p_7) +{ /* block id: 483 */ + uint32_t l_755 = 0UL; + struct S2 l_758 = {{-918,375,16},0xEEC353FBL,{-1257,392,8},2UL,6UL,0x5AFAA565L,{7UL,310},{738,206,0},1UL}; + union U3 l_761[3] = {{-1L},{-1L},{-1L}}; + uint32_t l_762 = 4294967286UL; + uint8_t *l_763 = &g_397[6][7]; + int32_t *l_764 = (void*)0; + int32_t *l_765 = &g_285[0]; + int32_t l_770 = (-10L); + struct S2 *l_772 = (void*)0; + struct S2 **l_771[1][4]; + int32_t l_799 = 0x7D10B75FL; + int32_t l_800 = (-6L); + int32_t l_801[9] = {9L,9L,(-9L),0x1EECED55L,(-9L),9L,(-9L),9L,0x1EECED55L}; + uint8_t l_809 = 0x7BL; + struct S0 **l_814 = &g_259; + int16_t *l_850 = &g_254[9]; + uint8_t l_865[6][10] = {{9UL,4UL,0UL,0x85L,1UL,0xFDL,0x85L,0x85L,1UL,9UL},{0x85L,9UL,9UL,1UL,1UL,0x85L,0x85L,0UL,0x85L,4UL},{9UL,1UL,0x85L,9UL,9UL,1UL,0UL,0x85L,0x85L,0xFDL},{9UL,0x85L,4UL,4UL,1UL,4UL,1UL,4UL,0xFDL,1UL},{1UL,1UL,4UL,0xFDL,1UL,1UL,9UL,1UL,0x85L,0UL},{0UL,9UL,0UL,1UL,0UL,4UL,9UL,9UL,0x85L,9UL}}; + uint32_t l_899 = 0xBDBB613DL; + int32_t l_908 = 0x1E925F72L; + uint8_t l_930 = 255UL; + struct S0 ****l_975 = (void*)0; + const union U3 l_1048 = {0x9EL}; + int i, j; + for (i = 0; i < 1; i++) + { + for (j = 0; j < 4; j++) + l_771[i][j] = &l_772; + } + l_755--; + (*l_765) &= (l_758 , (((~g_60.f2) , &g_739[5]) != &g_254[6])); + (*l_765) = func_24((safe_mod_func_uint8_t_u_u(g_70.f3, (safe_rshift_func_uint8_t_u_u(((((p_6.f2 > ((p_6.f0 ^ (*l_765)) ^ p_6.f3)) , l_758) , p_6.f3) ^ ((l_758.f6 , ((l_770 , l_771[0][3]) == (void*)0)) <= p_6.f0)), 7)))), l_758.f0, l_761[1], g_70.f3, l_761[1]); + if ((*l_765)) + { /* block id: 489 */ + int16_t l_781 = 0x1E28L; + uint8_t **l_783 = (void*)0; + uint8_t ***l_782 = &l_783; + int32_t l_794 = (-1L); + int32_t l_796[1]; + struct S1 *l_807 = &l_758.f6; + int32_t *l_808[7][5] = {{(void*)0,&g_8,&l_799,&l_799,&l_796[0]},{(void*)0,(void*)0,&l_799,&g_285[0],(void*)0},{&g_8,&l_794,(void*)0,&l_799,(void*)0},{(void*)0,&l_794,&l_796[0],&g_8,&g_8},{(void*)0,&g_285[0],(void*)0,&l_799,(void*)0},{&l_794,(void*)0,&l_794,(void*)0,(void*)0},{&g_285[0],&g_285[0],(void*)0,(void*)0,&l_794}}; + int i, j; + for (i = 0; i < 1; i++) + l_796[i] = 2L; + p_6.f1 = p_3; + if ((safe_mod_func_int16_t_s_s((g_70.f6.f1 | (safe_rshift_func_int8_t_s_u((safe_add_func_int8_t_s_s((safe_rshift_func_int16_t_s_s(((*l_765) = l_781), 15)), ((((((*l_782) = (void*)0) != (void*)0) , (safe_unary_minus_func_int16_t_s(g_524.f2))) && ((l_781 && (((((p_7 <= p_6.f1) < (safe_sub_func_uint32_t_u_u((safe_div_func_int32_t_s_s((safe_add_func_int8_t_s_s(((0L > g_79) < 1UL), l_781)), g_524.f2)), 0xD5B1C253L))) > 0xE7AFE5B4L) & 0xB3L) & p_6.f1)) <= 0x4BA776FBL)) , p_6.f1))), g_304.f3))), p_3))) + { /* block id: 493 */ + return g_8; + } + else + { /* block id: 495 */ + int8_t l_797[1]; + int32_t l_798[9]; + uint32_t l_802 = 4294967295UL; + struct S1 *l_805 = (void*)0; + int i; + for (i = 0; i < 1; i++) + l_797[i] = (-1L); + for (i = 0; i < 9; i++) + l_798[i] = 0L; + for (g_351 = 10; (g_351 < 10); ++g_351) + { /* block id: 498 */ + int32_t *l_793 = &g_285[0]; + int32_t *l_795[2]; + struct S1 **l_806[4][10] = {{&g_156[2][2][2],&g_156[2][5][4],&g_156[3][4][1],&g_156[2][1][7],&g_156[2][5][4],&g_156[2][1][7],&g_156[3][1][3],&g_156[2][2][2],&g_156[3][4][1],&g_156[3][1][3]},{&g_156[3][1][3],&g_156[2][2][2],&g_156[2][1][7],&g_156[2][2][2],&g_156[2][2][2],&g_156[2][2][2],&g_156[2][5][4],&g_156[3][4][1],&g_156[2][1][7],&g_156[2][1][7]},{&g_156[2][1][7],&g_156[3][1][3],&g_156[2][5][4],&g_156[3][4][1],&g_156[3][1][3],&g_156[3][4][1],&g_156[2][2][2],&g_156[2][1][7],&g_156[2][5][4],&g_156[2][2][2]},{&g_156[2][2][2],&g_156[2][1][7],&g_156[3][4][1],&g_156[2][1][7],&g_156[2][1][7],&g_156[2][1][7],&g_156[3][1][3],&g_156[2][5][4],&g_156[3][4][1],&g_156[3][4][1]}}; + int i, j; + for (i = 0; i < 2; i++) + l_795[i] = (void*)0; + --l_802; + l_807 = l_805; + } + } + ++l_809; + } + else + { /* block id: 504 */ + uint32_t l_812 = 4UL; + struct S0 ***l_823 = &g_258; + union U3 l_828 = {-8L}; + struct S1 ***l_833[8][7] = {{(void*)0,&g_538[0],&g_538[0],&g_538[0],&g_538[0],&g_538[0],&g_538[0]},{&g_538[0],&g_538[0],&g_538[0],&g_538[0],&g_538[0],(void*)0,&g_538[0]},{&g_538[0],&g_538[0],&g_538[0],&g_538[0],(void*)0,&g_538[0],&g_538[0]},{&g_538[0],&g_538[0],&g_538[0],&g_538[0],&g_538[0],(void*)0,&g_538[0]},{(void*)0,(void*)0,&g_538[0],&g_538[0],&g_538[0],&g_538[0],&g_538[0]},{&g_538[0],&g_538[0],(void*)0,&g_538[0],&g_538[0],&g_538[0],&g_538[0]},{&g_538[0],&g_538[0],&g_538[0],&g_538[0],&g_538[0],&g_538[0],&g_538[0]},{&g_538[0],(void*)0,(void*)0,(void*)0,&g_538[0],&g_538[0],&g_538[0]}}; + int32_t l_857 = 0xBD36499BL; + int32_t l_895 = 0x949E899AL; + int32_t l_896 = 0xA891575AL; + int32_t l_897[3]; + int32_t *l_929[8][2] = {{(void*)0,(void*)0},{(void*)0,(void*)0},{(void*)0,(void*)0},{(void*)0,&l_801[0]},{&l_801[0],&l_801[0]},{(void*)0,(void*)0},{(void*)0,(void*)0},{(void*)0,(void*)0}}; + struct S2 *l_992 = &l_758; + uint8_t **l_1011 = &g_904; + struct S2 ***l_1061 = &l_771[0][3]; + uint8_t l_1101 = 0UL; + int i, j; + for (i = 0; i < 3; i++) + l_897[i] = 0x4C16D487L; + if (l_812) + { /* block id: 505 */ + int32_t l_813 = 1L; + struct S0 ***l_821[5][5] = {{&l_814,&l_814,(void*)0,&g_258,(void*)0},{&l_814,&g_258,&g_258,&g_258,&l_814},{(void*)0,&l_814,&l_814,&l_814,(void*)0},{&g_258,&l_814,&l_814,&l_814,&l_814},{&l_814,&g_258,(void*)0,&g_258,(void*)0}}; + struct S0 ****l_822 = &l_821[3][2]; + uint16_t *l_824[6][10][4] = {{{&g_235,(void*)0,&g_70.f3,&g_255},{&g_235,&g_70.f3,&g_255,(void*)0},{&g_255,&l_758.f3,(void*)0,&g_70.f3},{(void*)0,&g_79,(void*)0,&g_79},{&g_255,&g_235,&g_255,(void*)0},{&g_235,&g_79,&g_255,&g_235},{&g_235,&l_758.f3,(void*)0,&g_621[0][1][1]},{&g_255,&g_621[0][1][1],&g_621[0][1][1],&g_255},{&g_235,(void*)0,&g_621[0][1][1],(void*)0},{&g_235,(void*)0,&g_255,&g_235}},{{&l_758.f3,(void*)0,&g_235,&g_235},{&g_235,(void*)0,&g_79,&l_758.f3},{&g_235,&g_70.f3,&g_255,(void*)0},{&g_70.f3,&g_255,&g_70.f3,&g_235},{&g_255,&l_758.f3,&g_255,&l_758.f3},{&g_621[0][1][1],(void*)0,&g_255,&g_235},{&g_79,(void*)0,&g_235,&g_255},{&g_255,&g_235,&g_621[0][1][1],&g_621[0][1][1]},{&l_758.f3,&l_758.f3,&l_758.f3,&g_235},{&g_70.f3,&g_79,&l_758.f3,&l_758.f3}},{{(void*)0,&g_79,&g_255,(void*)0},{&g_621[0][1][1],&g_255,&g_235,&l_758.f3},{&g_621[0][1][1],&g_255,(void*)0,&g_621[0][1][1]},{(void*)0,&l_758.f3,&g_255,&l_758.f3},{&g_621[0][1][1],&g_235,&l_758.f3,&g_621[0][1][1]},{(void*)0,(void*)0,&g_621[0][1][1],&l_758.f3},{&g_79,&g_255,&l_758.f3,(void*)0},{&g_255,&g_255,&g_79,&g_255},{(void*)0,&g_621[0][1][1],&g_235,&g_621[0][1][1]},{&g_235,&g_255,&g_255,&g_235}},{{&g_79,&l_758.f3,&g_235,&g_70.f3},{&g_621[0][1][1],&g_621[1][1][1],(void*)0,(void*)0},{&g_621[1][1][1],&g_235,(void*)0,&g_621[0][1][1]},{&g_621[0][1][1],(void*)0,&l_758.f3,&g_621[0][1][1]},{&g_79,&g_621[0][1][1],&g_255,&g_235},{&g_255,(void*)0,(void*)0,&g_621[1][1][1]},{&g_621[0][1][1],&g_255,&g_255,(void*)0},{&g_235,&l_758.f3,&g_70.f3,&g_70.f3},{&g_235,&g_70.f3,(void*)0,&l_758.f3},{(void*)0,(void*)0,&g_255,&g_70.f3}},{{&l_758.f3,(void*)0,(void*)0,&g_255},{&g_255,&g_255,(void*)0,&g_255},{&g_79,&g_621[0][1][1],&g_79,&g_255},{&g_235,(void*)0,&g_235,(void*)0},{&g_621[0][1][1],&l_758.f3,(void*)0,(void*)0},{&g_255,&g_235,(void*)0,&l_758.f3},{&g_255,(void*)0,(void*)0,&g_255},{&g_255,(void*)0,&g_621[0][1][1],(void*)0},{(void*)0,(void*)0,&g_255,(void*)0},{&g_70.f3,&l_758.f3,&g_70.f3,&g_235}},{{&g_79,&g_621[1][1][1],&g_235,(void*)0},{(void*)0,(void*)0,&g_70.f3,&g_255},{&g_235,(void*)0,&g_621[0][1][1],&l_758.f3},{&g_621[0][1][1],&g_235,(void*)0,(void*)0},{&g_255,&g_235,&g_235,(void*)0},{(void*)0,(void*)0,&g_621[0][1][1],&g_255},{&g_235,&g_255,&g_621[0][1][1],&g_255},{&g_235,&g_621[0][1][1],(void*)0,&g_235},{&l_758.f3,(void*)0,&g_70.f3,&g_255},{&g_235,(void*)0,(void*)0,&l_758.f3}}}; + int32_t l_825 = (-3L); + union U3 *l_830[9][10] = {{&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0]},{&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0]},{&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0]},{&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0]},{&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0]},{&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0]},{&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0]},{&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0]},{&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0],&l_761[0]}}; + struct S1 l_846[3] = {{8UL,274},{8UL,274},{8UL,274}}; + const struct S2 *l_875 = &g_873[1]; + const struct S2 **l_874 = &l_875; + int32_t l_889 = 7L; + int32_t l_891 = 0xD6DC0909L; + int32_t l_893 = 0x73944F16L; + int32_t l_898 = 0xDC9222DBL; + uint8_t **l_905 = &l_763; + int i, j, k; + if ((func_24(l_813, (*g_113), (p_6 = p_6), g_449, l_761[0]) , p_7)) + { /* block id: 510 */ +lbl_937: + (*g_124) = &l_825; + } + else + { /* block id: 512 */ + struct S0 l_841 = {907,644,18}; + int32_t l_890 = 0xB398AC28L; + int32_t l_894[4][10] = {{(-10L),(-10L),0xC401171EL,(-1L),(-2L),(-2L),0x249327B8L,(-2L),1L,0x1A769766L},{0x1A769766L,0x249327B8L,0x1A769766L,(-2L),0x1A769766L,0xC401171EL,0x249327B8L,0x249327B8L,1L,0x249327B8L},{1L,0xC401171EL,0xC401171EL,(-2L),0xC401171EL,(-10L),0xC401171EL,(-1L),(-2L),(-2L)},{(-2L),0x1A769766L,(-10L),0x249327B8L,0x249327B8L,0xC401171EL,0x249327B8L,(-1L),0x249327B8L,(-2L)}}; + union U3 l_913 = {0x54L}; + struct S1 l_922[6] = {{0xAFL,120},{0xAFL,120},{0xAFL,120},{0xAFL,120},{0xAFL,120},{0xAFL,120}}; + int i, j; + for (g_489.f2 = (-26); (g_489.f2 <= 10); g_489.f2 = safe_add_func_uint16_t_u_u(g_489.f2, 4)) + { /* block id: 515 */ + struct S1 ****l_834 = &l_833[2][1]; + int32_t l_842 = 4L; + int32_t *l_843 = &l_801[0]; + (*l_843) |= func_24((((*l_834) = l_833[7][5]) == (void*)0), (*g_113), p_6, ((safe_mul_func_int16_t_s_s((safe_mul_func_int16_t_s_s((safe_mod_func_uint32_t_u_u(((func_24(p_4, l_841, p_6, p_4, (*g_664)) , 1UL) & 8UL), 6L)), 0UL)), p_5)) , l_842), p_6); + (*l_843) = (p_6.f2 | 0x93078A12L); + if (l_828.f2) + continue; + } + if ((&g_70 != (void*)0)) + { /* block id: 521 */ + int32_t *l_860 = (void*)0; + for (l_828.f3 = (-28); (l_828.f3 >= 25); l_828.f3 = safe_add_func_int32_t_s_s(l_828.f3, 8)) + { /* block id: 524 */ + int16_t **l_847 = (void*)0; + int16_t *l_849[6][4][6] = {{{&g_252,&g_254[9],&g_254[9],&g_739[3],&g_252,&g_254[6]},{&g_252,&g_252,&g_252,&g_252,(void*)0,&g_54[7][8]},{&g_54[7][8],&g_54[7][8],(void*)0,&g_252,&g_54[7][8],&g_54[7][8]},{&g_252,(void*)0,&g_739[3],&g_54[7][8],&g_252,&g_252}},{{(void*)0,&g_54[7][8],&g_252,&g_252,&g_254[6],&g_254[9]},{&g_254[9],&g_252,&g_254[6],(void*)0,&g_254[9],&g_252},{&g_54[7][8],&g_254[6],&g_54[7][8],&g_254[9],&g_252,(void*)0},{&g_254[6],&g_254[9],&g_54[7][8],&g_54[7][8],&g_252,&g_739[3]}},{{&g_739[3],&g_252,&g_252,&g_254[6],&g_739[3],&g_252},{&g_252,&g_252,&g_254[9],&g_739[3],&g_54[7][8],&g_254[6]},{&g_252,&g_739[3],&g_252,&g_252,(void*)0,&g_54[7][8]},{&g_54[7][8],&g_54[7][8],(void*)0,&g_252,&g_54[7][8],&g_54[7][8]}},{{&g_252,(void*)0,&g_739[3],&g_54[7][8],&g_252,&g_252},{&g_252,&g_54[7][8],&g_252,&g_739[3],&g_254[6],&g_254[9]},{&g_252,&g_252,&g_254[6],&g_252,&g_254[9],&g_252},{&g_54[7][8],&g_254[6],&g_54[7][8],&g_252,&g_252,(void*)0}},{{&g_252,&g_254[9],&g_54[7][8],&g_54[7][8],&g_252,&g_739[3]},{(void*)0,&g_252,&g_252,&g_252,&g_739[3],&g_252},{&g_254[9],&g_252,&g_254[9],(void*)0,&g_54[7][8],&g_254[6]},{&g_54[7][8],&g_739[3],&g_252,&g_254[9],(void*)0,&g_54[7][8]}},{{&g_254[6],&g_54[7][8],(void*)0,&g_54[7][8],&g_54[7][8],&g_54[7][8]},{&g_739[3],(void*)0,&g_739[3],&g_254[6],&g_252,&g_252},{&g_252,&g_54[7][8],&g_252,&g_739[3],&g_254[6],&g_254[9]},{&g_252,&g_252,&g_254[6],&g_252,&g_254[9],&g_252}}}; + int16_t **l_848 = &l_849[3][3][4]; + int i, j, k; + } + for (g_489.f3 = 0; (g_489.f3 >= (-14)); --g_489.f3) + { /* block id: 531 */ + (*g_124) = l_860; + } + } + else + { /* block id: 534 */ + int32_t *l_861 = &l_801[5]; + int32_t *l_862 = &l_799; + int32_t *l_863[8][3][2] = {{{&l_801[7],&l_825},{&l_801[0],&l_801[0]},{&l_801[7],&l_801[0]}},{{&l_801[0],&l_825},{&l_801[7],&l_825},{&l_801[0],&l_801[0]}},{{&l_801[7],&l_801[0]},{&l_801[0],&l_825},{&l_801[7],&l_825}},{{&l_801[0],&l_801[0]},{&l_801[7],&l_801[0]},{&l_801[0],&l_825}},{{&l_801[7],&l_825},{&l_801[0],&l_801[0]},{&l_801[7],&l_801[0]}},{{&l_801[0],&l_825},{&l_801[7],&l_825},{&l_801[0],&l_801[0]}},{{&l_801[7],&l_801[0]},{&l_801[0],&l_825},{&l_801[7],&l_825}},{{&l_801[0],&l_801[0]},{&l_801[7],&l_801[0]},{&l_801[0],&l_825}}}; + const struct S2 *l_872 = &g_873[1]; + const struct S2 **l_871[8] = {&l_872,&l_872,&l_872,&l_872,&l_872,&l_872,&l_872,&l_872}; + const struct S2 ***l_870 = &l_871[6]; + const struct S2 ***l_876 = &l_874; + uint32_t *l_879[10] = {&g_829[1],&g_829[1],&g_829[1],&g_829[1],(void*)0,&g_829[1],&g_829[1],&g_829[1],(void*)0,&g_829[1]}; + int i, j, k; + l_861 = &l_813; + l_865[1][4]--; + (*l_862) |= ((((((*l_870) = ((safe_mod_func_uint16_t_u_u(p_6.f2, 2UL)) , (void*)0)) != ((*l_876) = l_874)) && ((((g_361.f1 &= (safe_mod_func_int8_t_s_s((((l_813 |= l_828.f3) > p_6.f2) != g_67), g_70.f0.f0))) , (safe_sub_func_int32_t_s_s(((*l_765) = ((safe_mul_func_uint16_t_u_u((safe_sub_func_int8_t_s_s((g_489.f0 <= g_873[1].f2.f2), p_5)), g_739[5])) | g_8)), 4294967295UL))) & p_6.f3) , p_7)) > g_70.f0.f2) == g_739[4]); + } + for (l_758.f3 = 0; (l_758.f3 <= 0); l_758.f3 += 1) + { /* block id: 546 */ + uint32_t l_886 = 0xFF8F26A0L; + int32_t *l_887 = (void*)0; + int32_t *l_888[4] = {&l_800,&l_800,&l_800,&l_800}; + int32_t l_892 = 0x5B16BAD7L; + union U3 l_902 = {0xEAL}; + int8_t *l_914 = &l_828.f0; + int i; + l_758.f2.f0 |= ((*l_765) = l_886); + ++l_899; + for (l_809 = 0; (l_809 <= 0); l_809 += 1) + { /* block id: 552 */ + l_846[1] = func_19(l_825, (l_846[2].f0 , l_902), (g_903 != l_905), (((***l_823) , func_24(l_896, (***l_823), p_6, l_891, p_6)) <= 0UL)); + if (l_908) + continue; + } + l_922[4] = func_19((safe_sub_func_uint32_t_u_u((safe_sub_func_uint8_t_u_u(l_891, func_24((((l_913 , (((*l_914) = (0x68L != p_6.f2)) && p_6.f2)) , 0xA0L) == ((g_235--) && (safe_add_func_int16_t_s_s((g_873[1].f6.f0 | (safe_div_func_uint32_t_u_u(4294967295UL, l_895))), p_4)))), (**g_258), (*g_664), g_70.f6.f0, l_913))), 1UL)), p_6, p_4, g_921); + for (g_70.f3 = 0; (g_70.f3 <= 0); g_70.f3 += 1) + { /* block id: 561 */ + uint16_t l_923 = 0x3B19L; + int32_t * const l_926 = &l_801[0]; + int32_t **l_927 = (void*)0; + int32_t **l_928 = &l_888[0]; + (*l_765) ^= l_812; + ++l_923; + (*l_928) = l_926; + } + } + l_929[0][1] = ((*g_124) = &l_894[0][8]); + } + l_930++; + for (p_4 = 6; (p_4 >= 14); p_4 = safe_add_func_int8_t_s_s(p_4, 1)) + { /* block id: 573 */ + struct S1 l_940[5][2][4] = {{{{0x96L,209},{0x49L,359},{0x96L,209},{0x49L,359}},{{0x96L,209},{0x49L,359},{0x96L,209},{0x49L,359}}},{{{0x96L,209},{0x49L,359},{0x96L,209},{0x49L,359}},{{0x96L,209},{0x49L,359},{0x96L,209},{0x49L,359}}},{{{0x96L,209},{0x49L,359},{0x96L,209},{0x49L,359}},{{0x96L,209},{0x49L,359},{0x96L,209},{0x49L,359}}},{{{0x96L,209},{0x49L,359},{0x96L,209},{0x49L,359}},{{0x96L,209},{0x49L,359},{0x96L,209},{0x49L,359}}},{{{0x96L,209},{0x49L,359},{0x96L,209},{0x49L,359}},{{0x96L,209},{0x49L,359},{0x96L,209},{0x49L,359}}}}; + int i, j, k; + for (l_799 = 0; (l_799 <= (-15)); l_799 = safe_sub_func_uint32_t_u_u(l_799, 5)) + { /* block id: 576 */ + if (g_70.f0.f0) + goto lbl_937; + for (g_351 = 17; (g_351 >= 55); g_351 = safe_add_func_int8_t_s_s(g_351, 8)) + { /* block id: 580 */ + struct S1 l_941 = {0xAAL,212}; + int32_t l_948 = (-1L); + l_941 = l_940[4][1][2]; + (*l_765) = (+(l_948 |= (((l_825 == (safe_rshift_func_uint16_t_u_u(p_6.f2, 12))) , 0xB9584012L) == (safe_mul_func_uint16_t_u_u(((+(g_235 ^= (safe_sub_func_uint32_t_u_u(g_281, (0x1EL == (&p_7 == (void*)0)))))) ^ l_940[4][1][2].f1), g_60.f2))))); + } + (*g_112) = func_55(g_873[1].f8, func_55(g_873[1].f4, (**l_823))); + } + } + (*l_765) |= (&l_755 == &g_351); + } + else + { /* block id: 590 */ + int8_t *l_953 = (void*)0; + int8_t *l_954 = (void*)0; + int8_t *l_955 = &l_828.f3; + int8_t *l_956 = &l_761[1].f0; + int32_t l_958 = 0x9DAD464FL; + uint8_t ** const *l_963 = &g_903; + struct S2 *l_993 = &g_994; + int32_t l_999 = 0x1FC6B783L; + union U3 l_1000 = {2L}; + int32_t l_1004 = (-3L); + int32_t l_1023 = 4L; + int32_t l_1024 = 1L; + int32_t l_1025 = 0x8524DFEBL; + struct S1 l_1036[8][9] = {{{248UL,94},{0xF8L,338},{0xE8L,272},{0xF8L,338},{0x26L,289},{0xF8L,338},{253UL,183},{0x81L,69},{0xE8L,272}},{{0UL,304},{250UL,353},{246UL,358},{246UL,358},{0UL,304},{0x63L,23},{0UL,304},{0UL,304},{0UL,304}},{{248UL,80},{0xF8L,338},{253UL,183},{255UL,52},{0xE8L,272},{0x30L,130},{253UL,183},{255UL,52},{0x26L,289}},{{0x63L,23},{0UL,304},{246UL,358},{0x63L,23},{246UL,358},{250UL,353},{0x2FL,317},{0UL,304},{250UL,353}},{{0xE8L,272},{0xEAL,336},{0xE8L,272},{255UL,52},{253UL,183},{0x81L,69},{248UL,94},{0x81L,69},{0x26L,289}},{{0x2FL,317},{246UL,358},{0UL,304},{246UL,358},{246UL,358},{246UL,358},{250UL,353},{0x63L,23},{0UL,304}},{{0xE8L,272},{0x30L,130},{0x26L,289},{0xF8L,338},{0xE8L,272},{0x81L,69},{0xE8L,272},{0xEAL,336},{0xE8L,272}},{{0x63L,23},{246UL,358},{250UL,353},{0x2FL,317},{0UL,304},{250UL,353},{250UL,353},{0x2FL,317},{246UL,358}}}; + uint8_t l_1049 = 0xCDL; + uint16_t l_1052 = 4UL; + struct S1 *l_1053 = &g_70.f6; + struct S0 l_1102 = {810,622,17}; + int i, j; + if ((0UL && ((((safe_add_func_int32_t_s_s(((safe_unary_minus_func_uint16_t_u(p_5)) , 0x17D517E7L), 0xF3F259D8L)) || (255UL < ((*l_956) = ((*l_955) = (safe_unary_minus_func_int32_t_s((*l_765))))))) <= p_6.f0) && (!(((safe_unary_minus_func_uint32_t_u(g_54[6][1])) || (((((*l_850) = (*l_765)) > 0L) | l_958) > g_873[1].f0.f0)) || p_5))))) + { /* block id: 594 */ + int32_t *l_965 = &l_958; + int32_t l_977[9] = {1L,1L,1L,1L,1L,1L,1L,1L,1L}; + uint32_t l_1026 = 0xEA2335E4L; + int i; + if ((+(safe_mod_func_int16_t_s_s(g_285[0], g_873[1].f2.f1)))) + { /* block id: 595 */ + uint8_t ****l_961 = (void*)0; + uint8_t ** const **l_964 = &l_963; + (*l_765) = ((g_962 = &g_903) == ((*l_964) = l_963)); + } + else + { /* block id: 599 */ + uint32_t l_968 = 4294967295UL; + int32_t l_976 = 0x3FD06290L; + struct S0 ****l_996 = &l_823; + int32_t l_1019 = 0xF3E25FD9L; + (*g_124) = l_965; + if ((safe_sub_func_int8_t_s_s(p_4, ((void*)0 != &p_5)))) + { /* block id: 601 */ + struct S0 ****l_974 = &l_823; + uint32_t l_978[1]; + int i; + for (i = 0; i < 1; i++) + l_978[i] = 1UL; + (*g_124) = (*g_124); + l_968--; + (**g_124) = (safe_rshift_func_uint8_t_u_s(g_973, (l_974 == l_975))); + l_978[0]++; + } + else + { /* block id: 606 */ + union U3 *l_981 = &g_489; + union U3 *l_986 = &g_489; + uint32_t *l_989 = &g_217[4]; + uint32_t **l_988 = &l_989; + int32_t l_995 = (-1L); + uint32_t *l_1001 = (void*)0; + uint32_t *l_1002 = (void*)0; + uint32_t *l_1003 = &l_755; + l_981 = &p_6; + l_977[0] &= ((*l_765) < ((safe_rshift_func_uint16_t_u_s((safe_sub_func_uint32_t_u_u(g_70.f5, (((**g_124) <= ((l_828 = ((*l_986) = ((*l_981) = (*l_981)))) , g_987[0][0][0])) ^ ((l_976 , ((*l_988) = &l_762)) == &l_968)))), 8)) <= (safe_mul_func_int8_t_s_s((((**l_814) , g_829[1]) , g_987[0][0][0]), g_987[0][0][0])))); + l_993 = l_992; + l_1004 ^= ((((**g_124) = ((void*)0 != l_850)) != ((*l_1003) |= (p_6.f3 <= ((l_995 > (g_994.f4 & ((void*)0 == l_996))) == ((l_976 , ((safe_mod_func_int16_t_s_s(func_24(((func_19(p_4, p_6, l_999, g_70.f0.f2) , &g_962) != (void*)0), (*g_259), l_1000, g_994.f2.f2, p_6), 2L)) , l_995)) == l_995))))) , 0L); + } + for (l_908 = (-21); (l_908 >= (-30)); l_908 = safe_sub_func_int16_t_s_s(l_908, 2)) + { /* block id: 620 */ + int32_t l_1018 = 0x7BA02CBBL; + uint32_t l_1020 = 0xB5190745L; + (*g_124) = (void*)0; + l_1018 |= (safe_div_func_uint16_t_u_u((p_6.f0 >= (g_70.f6.f1 == (safe_div_func_uint16_t_u_u((((void*)0 != l_1011) == ((((safe_lshift_func_uint8_t_u_u(((*l_763) = ((safe_sub_func_int16_t_s_s((*l_965), p_7)) && (((safe_add_func_int16_t_s_s((func_24(p_6.f0, (p_6.f3 , (g_70 , (**l_814))), l_1000, g_873[1].f0.f0, p_6) < g_994.f8), p_5)) || 65531UL) && p_4))), 6)) , (void*)0) != &g_258) & p_6.f2)), 0xBE32L)))), p_6.f2)); + l_1020--; + l_1026++; + } + } + } + else + { /* block id: 628 */ + int8_t l_1050[1]; + uint32_t l_1051 = 0x8A9C3866L; + int32_t **l_1060 = &l_929[2][1]; + const struct S2 *l_1064 = &g_873[1]; + const struct S2 **l_1063[2][1]; + const struct S2 ***l_1062 = &l_1063[0][0]; + int32_t *l_1065 = (void*)0; + struct S0 l_1066 = {-1073,606,6}; + union U3 l_1090 = {-1L}; + int i, j; + for (i = 0; i < 1; i++) + l_1050[i] = 0xE6L; + for (i = 0; i < 2; i++) + { + for (j = 0; j < 1; j++) + l_1063[i][j] = &l_1064; + } + for (l_1004 = (-27); (l_1004 == (-28)); --l_1004) + { /* block id: 631 */ + int32_t l_1031 = 0x2CB25B1DL; + int32_t l_1047[1]; + int i; + for (i = 0; i < 1; i++) + l_1047[i] = 0L; + l_1051 |= (((l_1031 & (safe_mod_func_uint16_t_u_u((safe_add_func_int16_t_s_s((0UL == ((((l_1036[2][1] , ((g_254[9] || p_7) | (((safe_div_func_int16_t_s_s((p_5 ^= func_24((~(p_7 < (0x33L > (l_1047[0] = (func_24((safe_add_func_int8_t_s_s(((*l_955) = (safe_rshift_func_uint16_t_u_s((!(((***l_823) , ((*l_956) = (safe_div_func_int16_t_s_s(g_873[1].f5, g_747)))) && p_7)), p_6.f0))), 0x6FL)), (**g_258), p_6, p_6.f2, p_6) , 5L))))), (**g_112), p_6, g_70.f0.f0, l_1048)), g_873[1].f7.f0)) & 0xB1A9L) || l_1049))) == l_1050[0]) < p_6.f0) >= l_1036[2][1].f0)), (-1L))), p_6.f0))) | (-2L)) && l_958); + if (l_1052) + continue; + for (l_1000.f2 = 0; (l_1000.f2 <= 3); l_1000.f2 += 1) + { /* block id: 640 */ + union U3 *l_1055 = &l_761[2]; + union U3 **l_1054 = &l_1055; + l_1053 = l_1053; + if (l_958) + break; + (*l_1054) = &p_6; + } + } + (*l_765) ^= (((((((g_994.f0.f2 , g_70.f0.f2) , 0x98L) | g_70.f0.f0) > 0x78B4L) , 0xC9L) , (void*)0) != &g_156[2][1][7]); + if (p_6.f0) + { /* block id: 649 */ + struct S0 l_1067 = {-745,148,10}; + const union U3 l_1068 = {0xE6L}; + const union U3 l_1075 = {0xB9L}; + l_1066.f0 ^= (p_6.f0 | ((**l_1061) == ((safe_mul_func_uint16_t_u_u(func_24((safe_rshift_func_uint16_t_u_s(((safe_div_func_uint16_t_u_u((func_24((g_70 , g_397[6][7]), (***l_823), p_6, l_1067.f1, l_1075) == g_60.f1), l_1036[2][1].f1)) && p_4), g_70.f4)), (**g_112), l_828, p_6.f2, p_6), p_3)) , (**l_1061)))); + for (l_899 = 0; (l_899 != 57); l_899++) + { /* block id: 653 */ + return g_60.f1; + } + } + else + { /* block id: 656 */ + union U3 *l_1078 = &l_1000; + int32_t l_1083 = 0xDF239507L; + int32_t l_1091 = 0xD9436DE9L; + g_994.f2.f0 ^= func_24((((l_958 = 65527UL) || g_70.f0.f1) , g_70.f0.f0), func_30(p_7, l_761[1]), ((*l_1078) = p_6), (safe_add_func_int16_t_s_s((safe_add_func_uint8_t_u_u(l_1023, (l_999 , l_1083))), g_217[4])), p_6); + if (l_1083) + { /* block id: 660 */ + int8_t l_1084 = 0x40L; + (*g_124) = (*l_1060); + return l_1084; + } + else + { /* block id: 663 */ + const uint8_t l_1087 = 1UL; + uint32_t *l_1089 = &g_217[1]; + union U3 l_1092 = {-1L}; + struct S0 l_1103[3] = {{-1416,536,19},{-1416,536,19},{-1416,536,19}}; + int32_t l_1104 = (-8L); + int i; + (*l_765) = (safe_rshift_func_uint8_t_u_u(p_3, l_1087)); + (*l_1053) = (((*l_850) ^= g_304.f2) , func_19(((safe_unary_minus_func_int8_t_s((l_1091 |= (+func_24(((*l_1089) ^= p_3), (**g_112), l_1090, l_1023, p_6))))) <= (p_6 , ((void*)0 != &g_449))), l_1092, p_6.f2, p_7)); + (*g_124) = &g_285[4]; + l_1104 ^= (g_235 | func_24((func_24(((~((l_999 = func_24((p_6.f0 , (safe_div_func_int32_t_s_s(((safe_mul_func_int16_t_s_s(p_3, l_1092.f2)) == g_235), (-4L)))), l_1103[2], p_6, p_5, p_6)) , g_745)) || l_1036[2][1].f1), l_1066, l_828, p_6.f3, p_6) && 0x6F55L), (*g_113), p_6, g_255, p_6)); + } + } + (*g_124) = (*l_1060); + } + for (l_1101 = 0; l_1101 < 7; l_1101 += 1) + { + g_285[l_1101] = (-9L); + } + } + (*g_124) = (void*)0; + } + return g_873[1].f4; +} + + +/* ------------------------------------------ */ +/* + * reads : g_70.f3 g_371 g_252 g_254 g_285 g_70.f5 g_389 g_351 g_397 g_54 g_304 g_141 g_217 g_60.f0 g_70.f2.f0 g_304.f2 g_124 g_70 g_234 g_125 g_449 g_304.f3 g_489 g_258 g_259 g_203 g_112 g_113 g_235 g_255 + * writes: g_70.f3 g_371 g_252 g_285 g_70.f5 g_389 g_351 g_54 g_141 g_70.f2.f0 g_203 g_254 g_361 g_234 g_125 g_304.f3 g_489.f1 g_113 g_538 + */ +static union U3 func_10(int8_t p_11, struct S1 p_12, uint32_t p_13, uint16_t p_14) +{ /* block id: 245 */ + int32_t l_362 = 0xE1E14E08L; + int32_t l_366 = 0xFEEB1536L; + int32_t l_367 = 0xEBD9CC5EL; + int32_t l_369 = 0xC11021ADL; + int32_t l_370[1][1]; + int16_t l_378 = 6L; + union U3 l_399[7] = {{0L},{0L},{0L},{0L},{0L},{0L},{0L}}; + struct S1 l_411 = {0x94L,26}; + uint32_t l_412 = 1UL; + struct S2 l_419 = {{-1658,138,1},0x63A0385EL,{552,221,3},7UL,1UL,4294967295UL,{255UL,122},{68,637,6},4294967286UL}; + uint8_t *l_420 = &g_389[4]; + int32_t l_427 = 5L; + uint8_t l_441 = 1UL; + int16_t l_486 = (-6L); + uint32_t l_500 = 5UL; + struct S2 *l_518 = &g_70; + struct S2 **l_517[10][1] = {{&l_518},{(void*)0},{(void*)0},{(void*)0},{&l_518},{&l_518},{&l_518},{&l_518},{&l_518},{&l_518}}; + struct S1 **l_536 = &g_156[3][0][0]; + struct S0 ***l_539 = &g_258; + int32_t l_614 = (-1L); + int8_t l_618 = 0x00L; + int16_t l_619 = 1L; + int32_t l_704 = 0L; + uint32_t l_741 = 4294967286UL; + int i, j; + for (i = 0; i < 1; i++) + { + for (j = 0; j < 1; j++) + l_370[i][j] = 5L; + } +lbl_396: + for (g_70.f3 = 0; (g_70.f3 <= 9); g_70.f3 += 1) + { /* block id: 248 */ + int32_t *l_363 = &g_285[0]; + int32_t *l_364 = (void*)0; + int32_t *l_365[8] = {&l_362,&l_362,&l_362,&l_362,&l_362,&l_362,&l_362,&l_362}; + int32_t l_368[6] = {(-6L),(-6L),(-6L),(-6L),(-6L),(-6L)}; + int i; + g_371[3]--; + for (g_252 = 9; (g_252 >= 0); g_252 -= 1) + { /* block id: 252 */ + uint32_t l_374 = 0x66269D93L; + ++l_374; + } + g_285[5] |= (g_254[g_70.f3] == 0x67D3L); + for (g_70.f5 = 0; (g_70.f5 <= 5); g_70.f5 += 1) + { /* block id: 258 */ + int32_t l_377 = 0xDD09BC50L; + int8_t l_379 = 7L; + int32_t l_380 = 0xAE3961EAL; + int32_t l_381 = 0x42FC192AL; + int32_t l_382 = 7L; + int32_t l_383 = 0x9ACD6B56L; + int32_t l_384 = 0x3B7FE7ACL; + int32_t l_385 = 1L; + int32_t l_386 = 0x72941B8CL; + int32_t l_387 = 0x1F5CE4DAL; + int32_t l_388 = 0x3F1211D8L; + int i; + --g_389[4]; + } + } + for (l_369 = 0; (l_369 < 0); l_369 = safe_add_func_uint32_t_u_u(l_369, 1)) + { /* block id: 264 */ + struct S0 l_398 = {-1582,239,17}; + int8_t l_400 = 0xDBL; + uint8_t *l_407 = &g_203[9]; + struct S1 *l_408 = &g_361; + for (g_351 = (-14); (g_351 < 46); ++g_351) + { /* block id: 267 */ + if (g_252) + goto lbl_396; + if (g_397[6][7]) + break; + } + (*l_408) = func_19((g_54[7][8] ^= (l_398 , (p_12.f1 | p_13))), l_399[5], l_399[5].f3, ((*l_407) = (p_14 == (~((p_14 > l_400) | (safe_div_func_uint16_t_u_u(func_24((safe_rshift_func_uint16_t_u_u(((((safe_mod_func_uint32_t_u_u(p_14, 0x7A0AC41DL)) , 1UL) != l_398.f0) && 65527UL), p_13)), l_398, l_399[1], l_399[5].f2, g_304), p_14))))))); + } + if (((safe_mul_func_int16_t_s_s((l_411 , l_412), ((p_12.f1 || ((safe_add_func_uint16_t_u_u((&g_156[2][1][7] != ((l_370[0][0] <= ((p_12.f1 & (((safe_rshift_func_uint8_t_u_u(((*l_420) = (safe_add_func_int32_t_s_s((l_419 , (l_367 |= (0x12A3BDFEL != ((4L > p_14) , 4294967295UL)))), l_362))), p_12.f1)) | 0xA0E8L) & l_362)) | l_419.f7.f2)) , (void*)0)), 0x28D3L)) < l_419.f3)) == 0xC3C018DCL))) > p_11)) + { /* block id: 277 */ + int32_t l_424 = 0x94D545F5L; + int32_t l_431 = 0L; + int32_t l_433 = 0x2D02096CL; + int32_t l_439 = 9L; + int32_t l_440 = 0xE3C3D422L; + int16_t *l_446 = &g_54[6][7]; + union U3 l_450 = {-10L}; + uint32_t l_461[7] = {0x9D54AD48L,0UL,0x9D54AD48L,0UL,0x8A9DE29EL,0UL,0x8A9DE29EL}; + int8_t *l_487 = &g_304.f3; + union U3 *l_488 = &l_399[5]; + struct S1 l_509 = {246UL,50}; + int16_t l_542 = 0xB5B2L; + int i; + for (p_12.f0 = 0; (p_12.f0 <= 4); p_12.f0 += 1) + { /* block id: 280 */ + int32_t l_423[4][2] = {{0x80E682F7L,2L},{0x80E682F7L,2L},{0x80E682F7L,2L},{0x80E682F7L,2L}}; + int i, j; + for (g_234 = 2; (g_234 <= 8); g_234 += 1) + { /* block id: 283 */ + int8_t l_425 = 0x0AL; + int32_t l_426 = 0xFD18EFFAL; + int32_t l_430 = 0x97BB54BDL; + int32_t l_432 = 0x0CE76D96L; + int32_t l_434 = 0x76B98D45L; + int32_t l_438 = 0xF3D00703L; + for (g_70.f5 = 1; (g_70.f5 <= 4); g_70.f5 += 1) + { /* block id: 286 */ + int32_t *l_421 = &g_285[0]; + int32_t l_428 = 0L; + int32_t l_429 = 0x3C8A0015L; + int32_t l_435 = 5L; + int32_t l_436 = 1L; + int32_t l_437 = 0x0061209BL; + int i, j; + for (g_361.f0 = 0; (g_361.f0 <= 8); g_361.f0 += 1) + { /* block id: 289 */ + (*g_124) = (void*)0; + } + if ((g_70 , ((*l_421) ^= g_54[(g_234 + 1)][(g_70.f5 + 1)]))) + { /* block id: 293 */ + int32_t *l_422[7] = {&l_370[0][0],&l_370[0][0],&l_370[0][0],&l_370[0][0],&l_370[0][0],&l_370[0][0],&l_370[0][0]}; + int i, j; + l_423[2][0] &= ((*l_421) = g_54[(g_70.f5 + 4)][g_234]); + (*g_124) = l_422[4]; + ++l_441; + (**g_124) ^= g_397[g_234][g_70.f5]; + } + else + { /* block id: 299 */ + const struct S2 l_444 = {{-725,623,1},0xCA113D64L,{-36,526,18},0xFD0AL,2UL,0x325F462AL,{0x24L,77},{1998,387,3},4294967292UL}; + const int16_t *l_448[9][3] = {{&g_449,&g_449,&g_449},{&g_449,&g_449,&g_449},{&g_449,&g_449,&g_449},{&g_449,(void*)0,&g_449},{&g_449,&g_449,&g_449},{&g_449,(void*)0,&g_449},{&g_449,&g_449,&g_449},{&g_449,&g_449,(void*)0},{&g_449,&g_449,&g_449}}; + const int16_t **l_447 = &l_448[2][1]; + int i, j; + l_423[2][0] &= (g_389[g_70.f5] > (l_444 , ((safe_unary_minus_func_uint16_t_u((l_446 != ((*l_447) = &g_234)))) || g_389[p_12.f0]))); + (*g_124) = &g_8; + (*l_421) &= l_431; + } + } + } + for (l_419.f6.f0 = 3; (l_419.f6.f0 <= 8); l_419.f6.f0 += 1) + { /* block id: 309 */ + int i, j; + if (g_54[l_419.f6.f0][(p_12.f0 + 3)]) + break; + } + for (g_70.f5 = 0; (g_70.f5 <= 4); g_70.f5 += 1) + { /* block id: 314 */ + return l_450; + } + } + if ((l_446 != l_446)) + { /* block id: 318 */ + int32_t *l_451 = &l_370[0][0]; + int32_t *l_452 = &l_439; + int32_t *l_453 = &l_427; + int32_t *l_454 = (void*)0; + int32_t l_455 = 0L; + int32_t *l_456 = &l_366; + int32_t l_457 = 1L; + int32_t *l_458 = &l_427; + int32_t *l_459[9]; + int32_t l_460 = 0xC37D6EB4L; + int i; + for (i = 0; i < 9; i++) + l_459[i] = &l_427; + l_461[0]++; + } + else + { /* block id: 320 */ + uint16_t l_473 = 0x49B7L; + struct S1 **l_479[4]; + struct S1 ***l_478 = &l_479[2]; + int i; + for (i = 0; i < 4; i++) + l_479[i] = &g_156[2][1][7]; + (*g_124) = (g_304 , ((safe_rshift_func_uint16_t_u_u((&g_156[2][2][3] == ((*l_478) = ((l_419 , ((safe_unary_minus_func_int32_t_s((((l_419 , (safe_mod_func_int8_t_s_s((p_12.f1 || (safe_add_func_int32_t_s_s((safe_rshift_func_int8_t_s_u(l_473, 4)), ((safe_add_func_int16_t_s_s(0xF2F6L, (safe_add_func_uint16_t_u_u(g_449, (((((func_30((g_70.f3 <= 4294967288UL), g_304) , l_439) || 0x5133L) , l_473) && 0x3730C2AAL) , l_431))))) , g_285[0])))), g_70.f6.f1))) & p_14) , 0xF7A3E2A1L))) == p_12.f0)) , &g_156[0][0][6]))), 4)) , (void*)0)); + } + if ((safe_mul_func_uint16_t_u_u((safe_sub_func_int16_t_s_s(((((((l_419.f7.f1 | (safe_lshift_func_int8_t_s_u(l_486, 2))) | l_431) <= func_24(l_450.f3, func_30(((*l_487) ^= g_70.f6.f0), ((*l_488) = l_450)), g_489, (g_389[4] , l_362), g_489)) >= p_13) != 6L) | 0xAFF3E48AL), 0x0CA3L)), p_12.f1))) + { /* block id: 326 */ + struct S1 *l_492 = &l_419.f6; + int32_t l_499 = 0xD4FC97C9L; + const int32_t l_506 = 0xD029A9FEL; + struct S1 ***l_537[5][10][1] = {{{&l_536},{(void*)0},{&l_536},{&l_536},{&l_536},{(void*)0},{&l_536},{&l_536},{&l_536},{(void*)0}},{{&l_536},{&l_536},{&l_536},{&l_536},{&l_536},{&l_536},{&l_536},{&l_536},{&l_536},{(void*)0}},{{&l_536},{&l_536},{&l_536},{(void*)0},{&l_536},{&l_536},{&l_536},{(void*)0},{&l_536},{&l_536}},{{&l_536},{&l_536},{&l_536},{&l_536},{&l_536},{&l_536},{&l_536},{(void*)0},{&l_536},{&l_536}},{{&l_536},{(void*)0},{&l_536},{&l_536},{&l_536},{(void*)0},{&l_536},{&l_536},{&l_536},{&l_536}}}; + int i, j, k; + for (l_450.f0 = 0; (l_450.f0 < (-19)); l_450.f0 = safe_sub_func_uint8_t_u_u(l_450.f0, 3)) + { /* block id: 329 */ + g_489.f1 = ((**g_258) , 0x04E13DBBL); + } + (*l_492) = p_12; + for (g_141 = 0; (g_141 <= 9); g_141 += 1) + { /* block id: 335 */ + int32_t *l_493 = &l_431; + int32_t *l_494 = &l_427; + int32_t *l_495 = &l_370[0][0]; + int32_t *l_496 = &g_285[5]; + int32_t *l_497 = &l_433; + int32_t *l_498[2][6] = {{&l_440,(void*)0,&l_424,&l_424,&l_440,(void*)0},{(void*)0,&l_424,&l_440,&l_440,&l_424,&l_424}}; + struct S2 l_516 = {{-705,478,14},-2L,{874,439,19},0xA709L,0x881E990DL,0x2BEB2506L,{3UL,214},{1184,19,1},0UL}; + int i, j; + ++l_500; + for (l_439 = 0; (l_439 <= 3); l_439 += 1) + { /* block id: 339 */ + uint16_t *l_505 = (void*)0; + uint32_t l_531 = 0x08858FF4L; + int i; + (*l_493) &= (((g_254[(l_439 + 5)] = (safe_rshift_func_uint16_t_u_s(0xA922L, 9))) || ((l_499 = g_254[(l_439 + 5)]) <= (0L && l_506))) , ((~(*l_495)) & ((safe_add_func_uint32_t_u_u(g_70.f7.f2, (l_509 , (func_30(p_11, l_399[(l_439 + 2)]) , p_11)))) != 0x9845L))); + if (((((p_14 >= (safe_sub_func_uint32_t_u_u(((!(safe_mul_func_uint16_t_u_u((l_516 , (g_70.f0.f1 > ((void*)0 != &l_420))), ((g_70.f7.f2 , l_517[7][0]) != (void*)0)))) <= (p_12.f1 == l_419.f7.f0)), (*l_497)))) | g_54[7][8]) , l_419.f7.f2) != p_12.f0)) + { /* block id: 343 */ + int8_t l_519 = 1L; + int i; + (*l_492) = func_19(l_519, l_399[(l_439 + 3)], g_141, (safe_rshift_func_int8_t_s_u(l_424, 4))); + } + else + { /* block id: 345 */ + const union U3 *l_523 = &g_524; + const union U3 **l_522 = &l_523; + const union U3 *l_526[4]; + const union U3 **l_525 = &l_526[1]; + int i; + for (i = 0; i < 4; i++) + l_526[i] = &g_524; + (*l_525) = ((*l_522) = &g_304); + } + if (((p_14 <= ((safe_sub_func_uint8_t_u_u(((l_509.f0 | (p_11 > (safe_div_func_uint8_t_u_u(l_506, g_203[3])))) & ((1UL || (p_12.f1 , ((*l_420) |= ((0L ^ ((((l_531 < p_14) , l_509.f0) ^ l_419.f7.f1) && l_399[(l_439 + 2)].f0)) < 0xFFL)))) ^ g_70.f0.f0)), p_12.f0)) | p_11)) , l_399[(l_439 + 2)].f2)) + { /* block id: 350 */ + (*l_495) |= 0xA66C8E15L; + if (g_54[7][8]) + continue; + } + else + { /* block id: 353 */ + struct S1 l_532[4][5] = {{{248UL,224},{0x00L,328},{248UL,224},{0x00L,328},{248UL,224}},{{1UL,256},{1UL,256},{0x44L,66},{0x44L,66},{1UL,256}},{{1UL,32},{0x00L,328},{1UL,32},{0x00L,328},{1UL,32}},{{1UL,256},{0x44L,66},{0x44L,66},{1UL,256},{1UL,256}}}; + int i, j; + if (p_12.f0) + break; + (*l_492) = l_532[1][4]; + (*g_112) = (*g_112); + (*l_494) &= p_11; + } + (*g_124) = &g_285[0]; + for (l_419.f1 = 0; (l_419.f1 <= 3); l_419.f1 += 1) + { /* block id: 362 */ + uint32_t l_533 = 0x67712ECBL; + l_533--; + } + } + } + g_538[0] = l_536; + } + else + { /* block id: 368 */ + int16_t *l_543 = &g_252; + int32_t l_544 = (-2L); + p_12 = func_19(((l_539 == ((p_13 || ((((safe_rshift_func_int16_t_s_s(0x08C2L, 10)) > l_542) > (&g_234 != (g_235 , l_543))) & (&g_281 == (l_544 , (void*)0)))) , (void*)0)) > g_255), g_304, p_11, p_12.f1); + p_12 = l_509; + } + } + else + { /* block id: 372 */ + struct S1 l_550 = {255UL,4}; + union U3 l_567 = {0xA4L}; + int32_t l_579 = 1L; + int32_t l_607 = 0x343D702DL; + int32_t l_612[7] = {(-1L),(-1L),(-1L),1L,(-1L),(-1L),(-1L)}; + int32_t l_615 = 0x18B8815EL; + struct S0 l_644[9][3][6] = {{{{-416,386,14},{-353,567,15},{386,372,5},{755,243,19},{1570,267,4},{-1320,589,0}},{{1412,38,7},{-642,380,10},{1570,267,4},{-1688,534,20},{648,78,16},{449,197,0}},{{755,243,19},{537,229,7},{3,417,2},{-353,567,15},{-168,240,13},{449,197,0}}},{{{-1320,589,0},{1069,372,3},{1570,267,4},{1272,217,19},{3,417,2},{515,645,21}},{{1032,590,8},{-194,10,0},{-416,386,14},{836,386,12},{346,610,7},{346,610,7}},{{-353,567,15},{755,243,19},{755,243,19},{-1832,170,15},{755,243,19},{-1320,589,0}}},{{{1069,372,3},{-1832,170,15},{-353,567,15},{-642,380,10},{-1688,534,20},{1272,217,19}},{{3,417,2},{386,372,5},{-194,10,0},{1032,590,8},{-353,567,15},{1232,130,10}},{{3,417,2},{-416,386,14},{-105,256,14},{-642,380,10},{1272,217,19},{-168,240,13}}},{{{1069,372,3},{-1688,534,20},{-1974,600,18},{-1832,170,15},{836,386,12},{1069,372,3}},{{-353,567,15},{1412,38,7},{-100,325,0},{836,386,12},{-1832,170,15},{449,197,0}},{{1069,372,3},{1232,130,10},{648,78,16},{449,197,0},{-642,380,10},{1942,177,18}}},{{{515,645,21},{1942,177,18},{1272,217,19},{1602,197,15},{1032,590,8},{-416,386,14}},{{-1320,589,0},{1942,177,18},{449,197,0},{1232,130,10},{-642,380,10},{-1832,170,15}},{{1570,267,4},{1232,130,10},{515,645,21},{-1320,589,0},{-1832,170,15},{648,78,16}}},{{{-194,10,0},{1412,38,7},{386,372,5},{386,372,5},{836,386,12},{386,372,5}},{{3,417,2},{-1688,534,20},{3,417,2},{836,386,12},{1272,217,19},{1570,267,4}},{{449,197,0},{-416,386,14},{-1975,372,0},{648,78,16},{1602,197,15},{3,417,2}}},{{{1232,130,10},{-416,386,14},{-1975,372,0},{755,243,19},{1232,130,10},{1570,267,4}},{{-1975,372,0},{-353,567,15},{3,417,2},{537,229,7},{-1320,589,0},{386,372,5}},{{537,229,7},{-1320,589,0},{386,372,5},{-1832,170,15},{386,372,5},{648,78,16}}},{{{-1688,534,20},{1942,177,18},{515,645,21},{1032,590,8},{836,386,12},{-1832,170,15}},{{346,610,7},{-100,325,0},{449,197,0},{515,645,21},{648,78,16},{-416,386,14}},{{-642,380,10},{1032,590,8},{449,197,0},{515,645,21},{755,243,19},{-105,256,14}}},{{{346,610,7},{-1240,227,3},{755,243,19},{1032,590,8},{537,229,7},{-1974,600,18}},{{-1688,534,20},{1602,197,15},{-642,380,10},{-1832,170,15},{-1832,170,15},{-100,325,0}},{{537,229,7},{537,229,7},{-1975,372,0},{537,229,7},{1032,590,8},{648,78,16}}}}; + int32_t l_674 = 0x3894630CL; + uint32_t l_691[7] = {4294967294UL,4294967294UL,4294967294UL,4294967294UL,4294967294UL,4294967294UL,4294967294UL}; + const int32_t *l_708 = (void*)0; + const int32_t *l_709 = &l_362; + int i, j, k; + l_419.f2.f0 ^= 0x0E521611L; + for (g_234 = 19; (g_234 <= 21); g_234++) + { /* block id: 376 */ + union U3 l_555 = {6L}; + int32_t l_568 = 0x3DA5E9D1L; + union U3 l_576 = {-1L}; + uint32_t l_578[4] = {4294967288UL,4294967288UL,4294967288UL,4294967288UL}; + struct S0 l_593 = {125,387,1}; + int32_t l_613 = 0xA08FAB05L; + int32_t l_617 = 8L; + const union U3 **l_665 = &g_664; + const union U3 l_675 = {1L}; + int32_t l_727 = (-10L); + int32_t l_728 = 0xB2D0C4B9L; + int32_t l_731 = 0x2C7BC777L; + int32_t l_732 = (-1L); + int32_t l_733 = (-1L); + int32_t l_735 = 0L; + int32_t l_736 = 0x63764777L; + int32_t l_737 = (-6L); + int32_t l_738[3]; + int16_t l_740[3][5][5] = {{{1L,3L,0x58C2L,0L,1L},{0x9C69L,0x938FL,0x58C2L,(-1L),0xDF69L},{0x58C2L,0L,0x37AAL,0x37AAL,0x7061L},{(-2L),(-1L),1L,(-1L),1L},{0x58C2L,(-1L),1L,3L,0xB254L}},{{0xD5DDL,9L,0x7061L,0xB254L,0x9C69L},{1L,(-1L),0x9C69L,0xB254L,0L},{(-1L),1L,0x318EL,0xB254L,(-1L)},{9L,0xD5DDL,0x37AAL,3L,0x37AAL},{0x58C2L,0x58C2L,9L,0x58C2L,(-1L)}},{{(-1L),0xD5DDL,(-1L),1L,3L},{0x7061L,0L,0x58C2L,1L,0xB254L},{1L,1L,0x3AA0L,0x7061L,0xB254L},{(-1L),9L,0xF9C9L,(-1L),0xB254L},{0x318EL,0xB254L,9L,3L,3L}}}; + int32_t l_746 = 0xF7B220A1L; + uint32_t *l_753 = &l_578[1]; + int i, j, k; + for (i = 0; i < 3; i++) + l_738[i] = 1L; + for (p_14 = (-16); (p_14 == 54); p_14 = safe_add_func_uint32_t_u_u(p_14, 3)) + { /* block id: 379 */ + struct S1 *l_549 = (void*)0; + l_550 = p_12; + } + } + } + return l_399[6]; +} + + +/* ------------------------------------------ */ +/* + * reads : + * writes: g_254 + */ +static struct S1 func_19(int16_t p_20, union U3 p_21, uint8_t p_22, uint8_t p_23) +{ /* block id: 240 */ + struct S1 l_359 = {255UL,212}; + l_359 = l_359; + for (p_23 = 0; p_23 < 10; p_23 += 1) + { + g_254[p_23] = 0x93DCL; + } + return l_359; +} + + +/* ------------------------------------------ */ +/* + * reads : g_252 g_141 g_70.f3 g_285 g_217 g_60.f0 g_70.f2.f0 g_304.f2 g_351 + * writes: g_252 g_141 g_70.f3 g_285 g_351 g_70.f2.f0 + */ +static uint16_t func_24(uint32_t p_25, struct S0 p_26, union U3 p_27, uint32_t p_28, const union U3 p_29) +{ /* block id: 200 */ + uint32_t l_317 = 0x2C0D943BL; + int32_t l_325 = 1L; + int32_t l_326 = 0xFC5A9C8AL; + int32_t l_327[4] = {2L,2L,2L,2L}; + const uint16_t *l_347 = &g_70.f3; + uint8_t l_357 = 1UL; + int32_t *l_358[10][7] = {{&g_285[0],&l_325,&g_285[6],&g_285[6],&l_327[0],(void*)0,&g_285[0]},{&g_285[0],&l_326,&g_285[1],(void*)0,&g_285[5],&g_8,&l_327[0]},{&g_285[6],&g_8,&g_285[0],&g_285[6],&l_325,(void*)0,&l_326},{&l_327[0],&l_327[0],&g_8,&g_8,&g_285[1],&l_327[0],&l_326},{&g_8,&l_327[0],&g_285[0],(void*)0,&l_326,&g_8,&l_325},{(void*)0,&l_325,&g_285[0],&g_285[6],&l_327[0],&g_8,&g_285[5]},{&g_285[5],(void*)0,(void*)0,&g_285[0],&l_327[0],(void*)0,&g_285[0]},{&g_285[0],&g_285[2],(void*)0,&l_327[0],&l_327[0],&g_8,&g_8},{&g_285[6],&l_327[0],&l_325,&l_325,&g_8,&g_285[6],&l_326},{&g_8,&g_285[2],&l_327[0],&l_325,&g_285[2],&g_8,&g_285[0]}}; + int i, j; + for (g_252 = 0; (g_252 <= 27); g_252++) + { /* block id: 203 */ + uint8_t l_315 = 0x02L; + l_315 = (-1L); + if (l_315) + continue; + } + for (g_141 = 0; (g_141 <= 6); g_141 += 1) + { /* block id: 209 */ + uint16_t l_316 = 65535UL; + uint32_t *l_350 = &g_351; + uint16_t l_352[7][5] = {{0xA96EL,0xDB85L,0UL,65527UL,0xDB85L},{65530UL,0x12FEL,0x616FL,0x41E1L,0x12FEL},{2UL,0UL,65527UL,0xA96EL,0UL},{0x616FL,0xC937L,0x12FEL,65530UL,0xC937L},{0xDB85L,2UL,0xA96EL,2UL,2UL},{0x12FEL,0x41E1L,65530UL,0x616FL,0x41E1L},{0xA96EL,0xDB85L,2UL,0xDB85L,0xDB85L}}; + struct S2 *l_354 = &g_70; + struct S2 **l_353 = &l_354; + int i, j; + for (g_70.f3 = 1; (g_70.f3 <= 4); g_70.f3 += 1) + { /* block id: 212 */ + uint8_t l_329 = 1UL; + int i; + if (g_285[g_141]) + { /* block id: 213 */ + int i; + return g_217[g_70.f3]; + } + else + { /* block id: 215 */ + int32_t l_328[5][4] = {{0L,(-1L),0L,(-1L)},{0L,(-1L),0L,(-1L)},{0L,(-1L),0L,(-1L)},{0L,(-1L),0L,(-1L)},{0L,(-1L),0L,(-1L)}}; + struct S1 * const l_335 = &g_70.f6; + struct S1 l_336 = {0xC7L,109}; + int i, j; + if (((l_316 ^ l_317) >= p_25)) + { /* block id: 216 */ + int32_t *l_318 = &g_285[5]; + int32_t *l_319 = &g_285[3]; + int32_t *l_320 = &g_285[g_141]; + int32_t *l_321 = &g_285[0]; + int32_t *l_322 = &g_285[0]; + int32_t *l_323 = &g_285[6]; + int32_t *l_324[2][6][1] = {{{&g_285[6]},{&g_8},{&g_8},{&g_285[0]},{&g_8},{&g_285[g_141]}},{{&g_285[5]},{&g_285[6]},{&g_8},{&g_285[5]},{&g_285[5]},{&g_285[0]}}}; + int i, j, k; + ++l_329; + for (p_27.f0 = 4; (p_27.f0 >= 0); p_27.f0 -= 1) + { /* block id: 220 */ + struct S1 *l_334[7][9] = {{&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6,(void*)0},{&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6,(void*)0},{&g_70.f6,(void*)0,&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6},{&g_70.f6,(void*)0,&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6},{&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6},{(void*)0,&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6},{&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6}}; + int i, j; + (*l_323) |= (safe_mul_func_int16_t_s_s((((void*)0 == &g_252) == ((l_334[3][5] != l_335) <= (l_336 , g_60.f0))), 0x9EE5L)); + l_319 = &g_285[g_141]; + } + } + else + { /* block id: 224 */ + return g_252; + } + } + if (l_325) + break; + } + g_285[g_141] = (safe_add_func_int8_t_s_s((((func_30((p_27.f3 = (safe_add_func_uint32_t_u_u(((*l_350) = ((g_285[g_141] <= (g_285[g_141] , (safe_mul_func_int8_t_s_s(g_285[g_141], (safe_div_func_uint8_t_u_u((safe_add_func_int16_t_s_s(((void*)0 == l_347), ((safe_lshift_func_int8_t_s_s(g_285[g_141], p_26.f1)) < ((p_29.f0 , p_25) <= g_60.f0)))), 1UL)))))) >= 251UL)), 0x952115EFL))), p_29) , 4L) , l_326) <= p_26.f0), l_352[2][1])); + (*l_353) = (void*)0; + p_26.f0 |= (safe_add_func_uint32_t_u_u((((*l_350) = (0x42L | ((-1L) == l_327[0]))) , ((*l_350) |= g_304.f2)), l_357)); + } + l_358[9][0] = &l_327[1]; + return p_27.f3; +} + + +/* ------------------------------------------ */ +/* + * reads : g_70.f3 g_70.f2.f0 + * writes: g_70.f3 g_70.f2.f0 + */ +static struct S0 func_30(const int8_t p_31, union U3 p_32) +{ /* block id: 192 */ + int32_t l_310 = (-1L); + struct S0 l_311 = {-1116,303,5}; + for (g_70.f3 = 0; (g_70.f3 == 52); g_70.f3 = safe_add_func_uint32_t_u_u(g_70.f3, 5)) + { /* block id: 195 */ + uint32_t l_307 = 0UL; + int32_t *l_308 = (void*)0; + int32_t *l_309[5][9][3] = {{{&g_8,&g_285[0],&g_8},{&g_8,&g_8,(void*)0},{&g_8,(void*)0,&g_8},{&g_8,&g_285[0],&g_285[0]},{&g_8,(void*)0,&g_285[0]},{&g_8,&g_8,&g_8},{&g_8,&g_8,(void*)0},{&g_8,(void*)0,(void*)0},{&g_8,&g_285[0],&g_8}},{{&g_8,(void*)0,&g_8},{&g_8,&g_8,(void*)0},{&g_8,&g_8,&g_285[0]},{&g_8,(void*)0,(void*)0},{&g_8,&g_285[0],&g_8},{&g_8,(void*)0,&g_8},{&g_8,&g_8,(void*)0},{&g_8,&g_8,&g_285[0]},{(void*)0,(void*)0,(void*)0}},{{(void*)0,&g_285[0],&g_8},{&g_8,&g_285[0],&g_8},{&g_8,&g_8,(void*)0},{&g_8,&g_8,&g_285[0]},{(void*)0,&g_285[0],(void*)0},{(void*)0,&g_8,&g_8},{&g_8,&g_285[0],&g_8},{&g_8,&g_8,&g_285[0]},{&g_8,&g_8,&g_8}},{{(void*)0,&g_285[0],&g_285[0]},{(void*)0,&g_8,&g_8},{&g_8,&g_285[0],&g_8},{&g_8,&g_8,&g_285[0]},{&g_8,&g_8,&g_8},{&g_285[0],&g_285[0],&g_285[0]},{&g_285[0],&g_8,&g_8},{&g_8,&g_285[0],&g_8},{(void*)0,&g_8,&g_285[0]}},{{&g_8,&g_8,&g_8},{&g_285[0],&g_8,&g_285[0]},{&g_285[0],&g_8,&g_8},{&g_8,&g_8,&g_8},{(void*)0,&g_8,&g_285[0]},{&g_8,&g_8,&g_8},{&g_285[0],&g_8,&g_8},{&g_285[0],&g_8,&g_8},{&g_8,&g_8,&g_8}}}; + int i, j, k; + g_70.f2.f0 |= (p_32 , p_32.f2); + l_310 |= p_31; + } + return l_311; +} + + +/* ------------------------------------------ */ +/* + * reads : g_8 g_54 g_60.f0 g_70 g_79 g_112 g_124 g_125 g_141 g_156 g_67 g_60.f2 g_203 g_217 g_235 g_255 g_258 g_252 g_259 g_281 g_285 g_304 + * writes: g_54 g_67 g_79 g_70.f2.f0 g_70.f6.f0 g_70.f6 g_60.f0 g_70.f5 g_141 g_125 g_203 g_217 g_70.f1 g_235 g_234 g_255 g_252 g_285 g_156 + */ +static union U3 func_33(int8_t p_34, int16_t p_35, int32_t p_36) +{ /* block id: 1 */ + int8_t l_44 = (-8L); + int16_t *l_52 = (void*)0; + int16_t *l_53 = &g_54[7][8]; + struct S1 *l_282 = (void*)0; + struct S1 **l_287 = &g_156[2][1][7]; + struct S1 **l_288 = &l_282; + int32_t l_289[5] = {1L,1L,1L,1L,1L}; + int32_t *l_290 = &g_285[0]; + int32_t *l_291 = (void*)0; + int32_t *l_292 = &l_289[0]; + int32_t *l_293 = &l_289[3]; + int32_t *l_294 = (void*)0; + int32_t *l_295 = &g_285[0]; + int32_t *l_296 = &g_285[0]; + int32_t *l_297 = &l_289[3]; + int32_t *l_298[1]; + int32_t l_299 = (-1L); + int32_t l_300 = 0x460548ABL; + uint16_t l_301 = 0x1E4AL; + int i; + for (i = 0; i < 1; i++) + l_298[i] = (void*)0; + (*l_288) = ((*l_287) = func_39(l_44, g_8, func_45((safe_lshift_func_uint16_t_u_s((safe_div_func_int8_t_s_s(p_35, 0x53L)), ((*l_53) = g_8))), g_8), l_282)); + l_301--; + return g_304; +} + + +/* ------------------------------------------ */ +/* + * reads : g_285 + * writes: g_285 + */ +static struct S1 * func_39(int32_t p_40, uint16_t p_41, struct S1 * p_42, struct S1 * p_43) +{ /* block id: 185 */ + uint32_t l_283 = 0x3C929064L; + int32_t *l_284 = &g_285[0]; + struct S1 *l_286 = (void*)0; + (*l_284) |= l_283; + return l_286; +} + + +/* ------------------------------------------ */ +/* + * reads : g_54 g_60.f0 g_70 g_8 g_79 g_112 g_124 g_125 g_141 g_156 g_60.f2 g_203 g_217 g_235 g_255 g_258 g_252 g_259 g_281 g_67 + * writes: g_67 g_79 g_70.f2.f0 g_70.f6.f0 g_70.f6 g_60.f0 g_70.f5 g_54 g_141 g_125 g_203 g_217 g_70.f1 g_235 g_234 g_255 g_252 + */ +static struct S1 * func_45(int32_t p_46, uint32_t p_47) +{ /* block id: 3 */ + struct S0 *l_59 = &g_60; + struct S0 **l_82 = &l_59; + int32_t * const l_83 = &g_8; + int32_t *l_85 = &g_8; + int32_t **l_84 = &l_85; + uint16_t l_105 = 0x9B7AL; + int32_t l_107[8] = {0xA1CE40C6L,0xA1CE40C6L,0xA1CE40C6L,0xA1CE40C6L,0xA1CE40C6L,0xA1CE40C6L,0xA1CE40C6L,0xA1CE40C6L}; + union U3 l_114 = {0xF1L}; + struct S1 l_247[7][10][1] = {{{{0x1EL,184}},{{0x79L,135}},{{5UL,49}},{{0xA4L,104}},{{0x43L,70}},{{0x25L,42}},{{0x43L,70}},{{0xA4L,104}},{{5UL,49}},{{0x79L,135}}},{{{0x1EL,184}},{{0xA4L,104}},{{252UL,79}},{{0x25L,42}},{{252UL,79}},{{0xA4L,104}},{{0x1EL,184}},{{0x79L,135}},{{5UL,49}},{{0xA4L,104}}},{{{0x43L,70}},{{0x25L,42}},{{0x43L,70}},{{0xA4L,104}},{{5UL,49}},{{0x79L,135}},{{0x1EL,184}},{{0xA4L,104}},{{252UL,79}},{{0x25L,42}}},{{{252UL,79}},{{0xA4L,104}},{{0x1EL,184}},{{0x79L,135}},{{5UL,49}},{{0xA4L,104}},{{0x43L,70}},{{0x25L,42}},{{0x43L,70}},{{0xA4L,104}}},{{{5UL,49}},{{0x79L,135}},{{0x1EL,184}},{{0xA4L,104}},{{252UL,79}},{{0x25L,42}},{{252UL,79}},{{0xA4L,104}},{{0x1EL,184}},{{0x79L,135}}},{{{5UL,49}},{{0xA4L,104}},{{0x43L,70}},{{0x25L,42}},{{0x43L,70}},{{0xA4L,104}},{{5UL,49}},{{0x79L,135}},{{0x1EL,184}},{{0xA4L,104}}},{{{252UL,79}},{{0x25L,42}},{{252UL,79}},{{0xA4L,104}},{{0x1EL,184}},{{0x79L,135}},{{5UL,49}},{{0xA4L,104}},{{0x43L,70}},{{0x25L,42}}}}; + int i, j, k; + (*l_82) = func_55((safe_unary_minus_func_uint8_t_u((l_59 != (void*)0))), &g_60); + (*l_84) = l_83; + if ((**l_84)) + { /* block id: 12 */ + uint16_t l_123[10][5]; + int32_t l_126 = 0L; + int i, j; + for (i = 0; i < 10; i++) + { + for (j = 0; j < 5; j++) + l_123[i][j] = 0xDDF1L; + } + for (p_46 = (-1); (p_46 < (-18)); p_46 = safe_sub_func_int16_t_s_s(p_46, 8)) + { /* block id: 15 */ + uint32_t l_90 = 0xCF63D7CBL; + int32_t l_103 = 0xA8BA3176L; + const struct S0 *l_111[5][10] = {{&g_70.f2,&g_70.f2,&g_70.f0,&g_60,&g_70.f2,&g_70.f0,&g_70.f0,&g_60,&g_70.f2,&g_60},{&g_60,&g_60,&g_70.f2,&g_70.f2,&g_70.f2,&g_70.f2,&g_70.f2,&g_70.f0,&g_60,&g_70.f0},{&g_70.f0,&g_70.f0,&g_70.f2,&g_70.f2,&g_60,&g_70.f2,&g_60,&g_70.f2,&g_70.f0,&g_70.f2},{&g_70.f2,&g_70.f2,&g_70.f2,&g_60,&g_70.f0,&g_70.f2,&g_70.f0,&g_70.f2,&g_70.f2,&g_60},{&g_70.f2,&g_70.f2,&g_70.f2,&g_70.f0,&g_70.f2,&g_60,&g_70.f2,&g_70.f2,&g_70.f2,&g_70.f0}}; + const struct S0 **l_110 = &l_111[2][5]; + struct S1 l_116 = {0xCAL,42}; + int i, j; + g_70.f2.f0 |= (safe_sub_func_uint32_t_u_u((0xE0L < (l_90 || g_70.f0.f0)), (-1L))); + if (((void*)0 == &g_54[7][8])) + { /* block id: 17 */ + int32_t l_97[7][8][4] = {{{(-3L),(-3L),0xFED72739L,0x8FEE3948L},{(-4L),0xBC662C87L,(-1L),(-1L)},{(-1L),4L,4L,(-3L)},{4L,(-3L),0x2ABD0CEAL,0xD32418DFL},{0xBC662C87L,0x429AA74CL,0x429AA74CL,0x8FEE3948L},{1L,0xD32418DFL,(-1L),0xC6490BB3L},{0x8DDDCD3EL,0xAAA56D44L,(-3L),0x8FEE3948L},{1L,(-3L),(-10L),0xAAA56D44L}},{{0xC6490BB3L,1L,1L,0xFED72739L},{(-1L),0xA2B653F5L,(-3L),0xA2B653F5L},{0x8AADD0F3L,6L,0x8FEE3948L,0xBC662C87L},{1L,(-4L),0xBC662C87L,(-3L)},{0xFED72739L,0x0324A0BDL,0xBC662C87L,6L},{1L,0xFED72739L,0xC6490BB3L,1L},{1L,0x2ABD0CEAL,0xD32418DFL,0xD32418DFL},{4L,4L,(-3L),0x2ABD0CEAL}},{{0x2ABD0CEAL,(-1L),(-1L),(-1L)},{(-4L),0xFED72739L,0x7B59BB01L,4L},{(-1L),0x429AA74CL,0L,4L},{0x429AA74CL,(-4L),0xAAA56D44L,(-1L)},{(-10L),(-3L),0x0324A0BDL,0x8DDDCD3EL},{1L,0xA2B653F5L,1L,0x7B59BB01L},{0x429AA74CL,6L,0x429AA74CL,(-3L)},{0x7B59BB01L,(-3L),0xAAA56D44L,(-10L)}},{{0xBC662C87L,4L,0x7B59BB01L,(-4L)},{0xBC662C87L,0xD32418DFL,0xAAA56D44L,(-10L)},{0x8FEE3948L,0x0324A0BDL,(-3L),(-1L)},{(-3L),(-3L),(-3L),0xD32418DFL},{(-1L),0xD32418DFL,(-1L),0xBC662C87L},{(-1L),0xC6490BB3L,0x8FEE3948L,(-10L)},{0xAAA56D44L,0x2ABD0CEAL,(-1L),0x0324A0BDL},{0xA2B653F5L,0x2ABD0CEAL,0xBC662C87L,(-10L)}},{{0xD32418DFL,0xBC662C87L,0L,0x2ABD0CEAL},{0x0324A0BDL,0xAAA56D44L,0x429AA74CL,0x7B59BB01L},{1L,1L,0xC6490BB3L,1L},{(-4L),(-1L),(-10L),(-1L)},{0xD32418DFL,0x8FEE3948L,(-1L),0xD32418DFL},{0xAAA56D44L,0x8AADD0F3L,(-1L),(-1L)},{0xAAA56D44L,0x7B59BB01L,(-10L),0x429AA74CL},{0x429AA74CL,(-1L),0x2ABD0CEAL,0x2ABD0CEAL}},{{(-1L),(-1L),0xD32418DFL,(-1L)},{(-1L),(-3L),1L,0x8AADD0F3L},{0x8FEE3948L,0x7B59BB01L,6L,(-3L)},{1L,0x8FEE3948L,1L,(-3L)},{0xC6490BB3L,0x7B59BB01L,6L,1L},{0xA2B653F5L,0xD32418DFL,0x8AADD0F3L,4L},{(-4L),1L,0x429AA74CL,6L},{0xC6490BB3L,4L,0x8FEE3948L,0xD32418DFL}},{{0x8DDDCD3EL,0xC6490BB3L,6L,0L},{0x0324A0BDL,(-1L),0x8DDDCD3EL,0x8FEE3948L},{0x0324A0BDL,0x2ABD0CEAL,6L,0xA2B653F5L},{(-10L),0x8AADD0F3L,0xC6490BB3L,0x429AA74CL},{0xBC662C87L,(-3L),(-3L),0x2ABD0CEAL},{(-3L),0xBC662C87L,(-1L),(-1L)},{0x8AADD0F3L,0x0324A0BDL,(-10L),0xA2B653F5L},{0x2ABD0CEAL,(-1L),0x429AA74CL,0L}}}; + uint16_t *l_102[8][9][3] = {{{(void*)0,&g_70.f3,&g_79},{&g_79,&g_70.f3,&g_70.f3},{&g_79,(void*)0,&g_70.f3},{&g_79,(void*)0,&g_70.f3},{&g_70.f3,(void*)0,&g_79},{(void*)0,&g_70.f3,&g_70.f3},{(void*)0,&g_70.f3,(void*)0},{&g_70.f3,(void*)0,&g_70.f3},{(void*)0,(void*)0,&g_79}},{{(void*)0,&g_70.f3,(void*)0},{(void*)0,(void*)0,&g_70.f3},{&g_79,&g_79,&g_70.f3},{&g_70.f3,(void*)0,&g_70.f3},{&g_79,&g_70.f3,(void*)0},{&g_79,&g_70.f3,&g_79},{&g_70.f3,&g_79,&g_70.f3},{&g_70.f3,&g_70.f3,&g_70.f3},{&g_70.f3,&g_70.f3,(void*)0}},{{&g_70.f3,&g_79,&g_79},{&g_70.f3,&g_70.f3,&g_79},{&g_79,&g_79,&g_79},{&g_79,&g_79,&g_79},{&g_79,&g_79,&g_70.f3},{(void*)0,&g_79,&g_79},{(void*)0,&g_79,(void*)0},{&g_70.f3,&g_79,&g_79},{&g_79,(void*)0,&g_70.f3}},{{(void*)0,&g_70.f3,&g_79},{&g_79,&g_79,&g_79},{&g_79,(void*)0,(void*)0},{&g_79,(void*)0,&g_79},{&g_79,(void*)0,&g_70.f3},{&g_79,(void*)0,&g_70.f3},{&g_70.f3,(void*)0,&g_79},{&g_79,&g_79,(void*)0},{&g_79,&g_70.f3,&g_79}},{{&g_79,&g_79,&g_79},{&g_70.f3,&g_70.f3,&g_70.f3},{(void*)0,(void*)0,&g_70.f3},{&g_79,(void*)0,&g_79},{&g_70.f3,&g_70.f3,&g_79},{(void*)0,&g_79,&g_70.f3},{&g_79,(void*)0,&g_79},{&g_70.f3,(void*)0,&g_79},{(void*)0,&g_79,&g_70.f3}},{{&g_70.f3,&g_70.f3,&g_70.f3},{&g_79,&g_70.f3,&g_70.f3},{&g_79,&g_79,&g_79},{&g_79,&g_70.f3,&g_79},{&g_79,&g_70.f3,&g_70.f3},{&g_79,&g_79,(void*)0},{&g_79,(void*)0,&g_79},{&g_79,&g_79,&g_79},{&g_70.f3,&g_79,(void*)0}},{{&g_70.f3,&g_70.f3,&g_70.f3},{(void*)0,(void*)0,&g_79},{&g_79,&g_79,&g_70.f3},{&g_79,(void*)0,(void*)0},{(void*)0,&g_79,(void*)0},{(void*)0,&g_70.f3,&g_70.f3},{&g_70.f3,&g_79,&g_70.f3},{(void*)0,&g_70.f3,&g_70.f3},{(void*)0,&g_79,&g_79}},{{&g_79,&g_70.f3,&g_79},{&g_79,(void*)0,&g_70.f3},{(void*)0,&g_79,(void*)0},{&g_79,(void*)0,(void*)0},{(void*)0,&g_70.f3,&g_79},{&g_79,&g_79,&g_70.f3},{&g_79,&g_79,&g_79},{&g_79,&g_79,(void*)0},{&g_79,(void*)0,&g_70.f3}}}; + uint8_t *l_104 = &g_70.f6.f0; + int i, j, k; + g_70.f2.f0 ^= (safe_lshift_func_int16_t_s_s(p_46, p_46)); + if (((0L & (safe_mul_func_int16_t_s_s((((*l_104) ^= ((safe_rshift_func_int8_t_s_s(((+p_46) , g_70.f0.f0), p_47)) < (0L && (l_97[2][3][1] ^ (p_47 < (safe_lshift_func_int16_t_s_s(((p_47 , (safe_mul_func_uint16_t_u_u((l_103 &= l_97[2][3][1]), p_47))) && (-8L)), 5))))))) , l_105), 0L))) , l_97[2][3][1])) + { /* block id: 21 */ + int32_t *l_106[7] = {&l_103,&l_97[0][2][3],&l_97[0][2][3],&g_8,&l_103,&l_103,&l_97[0][2][3]}; + int i; + l_107[5] = l_97[2][3][1]; + } + else + { /* block id: 23 */ + uint16_t l_115 = 1UL; + struct S1 *l_117 = &g_70.f6; + struct S1 *l_118 = &l_116; + int32_t l_128[4][4] = {{0xAE0EBF16L,1L,0xAE0EBF16L,1L},{0xAE0EBF16L,1L,0xAE0EBF16L,1L},{0xAE0EBF16L,1L,0xAE0EBF16L,1L},{0xAE0EBF16L,1L,0xAE0EBF16L,1L}}; + int32_t *l_129 = (void*)0; + int32_t *l_130 = &l_128[2][3]; + int32_t *l_131 = &l_126; + int32_t *l_132 = &l_97[3][6][0]; + int32_t *l_133 = &l_126; + int32_t *l_134 = &l_107[5]; + int32_t *l_135 = &l_97[5][3][3]; + int32_t *l_136 = &l_128[2][3]; + int32_t *l_137 = &l_126; + int32_t *l_138 = &l_126; + int32_t *l_139 = &l_103; + int32_t *l_140[5][10] = {{&l_107[6],&l_103,&g_8,&l_126,&l_103,&g_8,&l_126,(void*)0,&l_107[5],&g_8},{&l_107[5],&l_103,(void*)0,&l_103,&g_8,&l_97[2][3][1],&l_107[5],(void*)0,&l_126,&g_8},{&l_107[5],&l_126,&l_126,&l_107[5],(void*)0,&g_8,&g_8,&l_128[2][3],&l_103,&l_107[1]},{(void*)0,&l_107[5],(void*)0,(void*)0,(void*)0,&l_126,&g_8,&l_128[0][2],&l_107[5],&l_103},{&l_107[5],&g_8,(void*)0,&g_8,(void*)0,&l_126,&l_107[5],&l_126,(void*)0,&g_8}}; + int i, j; + if ((g_70.f7 , (safe_mul_func_uint16_t_u_u(g_54[2][3], ((l_110 != g_112) && ((p_47 == (l_114 , (l_115 & (p_46 ^ ((((((*l_118) = ((*l_117) = l_116)) , g_70.f4) > g_70.f2.f2) , p_47) & 0xF1956382L))))) > l_115)))))) + { /* block id: 26 */ + int16_t *l_121[6] = {&g_54[7][8],&g_54[7][8],&g_54[7][8],&g_54[7][8],&g_54[7][8],&g_54[7][8]}; + int16_t **l_120 = &l_121[4]; + uint32_t *l_122 = &g_70.f5; + int i; + g_60.f0 |= (safe_unary_minus_func_uint16_t_u(p_47)); + g_70.f2.f0 &= (((*l_120) = &g_54[7][8]) != &g_54[9][4]); + l_126 &= ((((*l_122) = (p_47 != (*l_85))) , ((**l_120) = ((((*l_104) = 0UL) > g_70.f7.f0) >= (l_123[6][1] != ((void*)0 == g_124))))) & p_46); + } + else + { /* block id: 34 */ + int32_t *l_127[4] = {&l_97[2][3][1],&l_97[2][3][1],&l_97[2][3][1],&l_97[2][3][1]}; + int i; + if ((**g_124)) + break; + g_70.f2.f0 &= l_116.f1; + } + --g_141; + } + l_103 |= 0x9AB640BFL; + l_97[4][3][2] |= (g_8 >= ((p_47 >= (safe_mod_func_uint32_t_u_u(l_103, 8UL))) < p_47)); + } + else + { /* block id: 42 */ + int32_t l_149[4] = {0xBC5006F5L,0xBC5006F5L,0xBC5006F5L,0xBC5006F5L}; + int i; + (*g_124) = (*g_124); + for (l_114.f2 = (-1); (l_114.f2 >= 24); ++l_114.f2) + { /* block id: 46 */ + int32_t *l_148[5]; + struct S1 *l_150 = (void*)0; + struct S1 *l_151 = &l_116; + int i; + for (i = 0; i < 5; i++) + l_148[i] = &l_107[5]; + l_149[0] &= (g_60.f0 |= p_46); + (*l_151) = l_116; + for (g_141 = 0; (g_141 >= 36); ++g_141) + { /* block id: 52 */ + if (p_47) + break; + (*l_151) = (g_70 , g_70.f6); + (*l_151) = g_70.f6; + } + } + for (l_105 = (-28); (l_105 != 42); l_105 = safe_add_func_uint32_t_u_u(l_105, 1)) + { /* block id: 60 */ + return g_156[2][1][7]; + } + } + } + } + else + { /* block id: 65 */ + uint32_t l_168 = 0UL; + int32_t l_174 = 8L; + int32_t l_206 = 0L; + int32_t l_215 = 0x8159E85BL; + uint16_t l_272 = 65534UL; + struct S0 *l_277 = &g_70.f2; + struct S2 l_280 = {{1515,507,0},0xA1E9C32DL,{1725,421,11},0xA2D4L,4294967293UL,4294967295UL,{0x17L,183},{209,374,19},0UL}; +lbl_268: + for (p_47 = (-26); (p_47 == 39); p_47++) + { /* block id: 68 */ + int32_t *l_159 = &g_8; + int32_t l_162 = 0L; + struct S1 l_178 = {252UL,162}; + int32_t l_214 = 0x89B60EE2L; + (*g_124) = ((*l_84) = l_159); + if (p_47) + { /* block id: 71 */ + int8_t l_164 = (-10L); + for (g_70.f6.f0 = 0; (g_70.f6.f0 <= 3); g_70.f6.f0 += 1) + { /* block id: 74 */ + if ((**g_124)) + break; + for (p_46 = 3; (p_46 >= 0); p_46 -= 1) + { /* block id: 78 */ + int32_t *l_160 = (void*)0; + int32_t *l_161 = &l_107[5]; + if ((*g_125)) + break; + (*l_161) |= (*g_125); + if ((*g_125)) + continue; + } + l_162 &= (*l_159); + for (g_67 = 0; (g_67 <= 3); g_67 += 1) + { /* block id: 86 */ + int32_t *l_163 = &l_107[5]; + int32_t *l_165 = &l_162; + int32_t *l_166 = &l_107[5]; + int32_t *l_167 = &l_162; + (*l_163) = 0x1E9C1B1BL; + l_168--; + } + } + } + else + { /* block id: 91 */ + uint32_t l_175 = 4294967291UL; + struct S1 *l_183 = &g_70.f6; + uint8_t *l_184 = (void*)0; + int32_t l_193 = 0x0082912AL; + int32_t l_216 = 4L; + if ((*l_85)) + { /* block id: 92 */ + int32_t *l_171 = (void*)0; + int32_t *l_172 = &l_107[4]; + int32_t *l_173[2][4]; + struct S1 *l_179[8] = {&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6,&g_70.f6}; + int i, j; + for (i = 0; i < 2; i++) + { + for (j = 0; j < 4; j++) + l_173[i][j] = &l_107[0]; + } + l_175--; + if ((**g_124)) + continue; + l_178 = l_178; + for (g_67 = 0; (g_67 < 1); g_67++) + { /* block id: 98 */ + struct S1 *l_182 = &l_178; + return l_183; + } + } + else + { /* block id: 101 */ + uint8_t **l_185 = (void*)0; + uint8_t **l_186 = &l_184; + int32_t l_197 = 0xDAA83F59L; + int32_t *l_198 = (void*)0; + int32_t *l_199 = &l_107[5]; + int32_t *l_200 = &l_174; + int32_t *l_201 = &l_193; + int32_t *l_202[6][10][4] = {{{(void*)0,(void*)0,&l_197,(void*)0},{(void*)0,&l_107[7],&l_197,&l_107[4]},{&l_197,&l_193,&l_107[0],&l_174},{&l_193,(void*)0,&l_162,&l_197},{&l_197,&l_162,&l_193,&l_107[4]},{&l_174,&l_197,&l_174,&l_197},{&l_174,&l_193,&l_107[0],(void*)0},{&l_107[4],&l_107[5],&l_162,&l_174},{&l_107[5],&l_174,&l_107[5],&l_174},{&l_107[5],&l_107[5],(void*)0,&l_107[5]}},{{&l_107[5],(void*)0,(void*)0,&g_8},{&l_174,&g_8,(void*)0,(void*)0},{&l_107[5],&l_162,&l_193,(void*)0},{&l_107[5],&l_107[5],(void*)0,&g_8},{&l_107[4],&l_107[5],&l_174,&l_107[1]},{(void*)0,&l_193,&l_107[4],&l_197},{&g_8,&g_8,&l_197,&l_107[5]},{&l_193,&l_107[7],(void*)0,&l_107[5]},{&l_107[5],&l_162,&l_107[5],(void*)0},{&l_107[1],&l_107[5],&l_197,&l_197}},{{&l_193,(void*)0,(void*)0,&l_193},{&l_174,&l_107[4],&l_162,&l_107[5]},{&l_193,&l_107[5],&l_197,&l_174},{&l_107[5],&l_193,&l_193,&l_107[5]},{&l_107[5],&l_193,&l_197,&g_8},{&l_193,&l_193,&l_107[4],(void*)0},{&g_8,(void*)0,(void*)0,&g_8},{(void*)0,&l_197,&l_107[0],&l_107[4]},{&l_107[5],&l_174,(void*)0,&l_162},{&l_107[4],&l_197,&l_162,&l_193}},{{&l_107[5],&l_107[5],(void*)0,&l_107[7]},{(void*)0,&l_193,(void*)0,&l_193},{(void*)0,&l_162,&l_197,&l_107[5]},{&l_107[5],&l_193,&l_107[7],&l_193},{&l_193,(void*)0,&l_197,&l_197},{(void*)0,&l_162,&l_174,&l_162},{&l_174,&l_193,&l_174,&g_8},{&l_107[5],(void*)0,&l_107[4],&l_162},{&l_107[7],&l_193,&l_107[5],&l_107[4]},{&l_193,&l_193,&l_197,&l_162}},{{(void*)0,(void*)0,&l_107[5],&l_107[7]},{&l_197,(void*)0,&l_174,&l_107[5]},{(void*)0,&l_107[5],&l_107[5],&l_107[5]},{&l_193,&l_174,&l_107[5],&l_193},{&l_107[4],&l_107[5],&l_197,(void*)0},{&l_174,&l_197,&l_193,(void*)0},{&l_193,&l_162,&g_8,&l_193},{(void*)0,(void*)0,&l_193,&l_193},{&l_197,&l_193,&l_107[5],&l_107[4]},{&l_107[5],&l_174,&l_107[1],(void*)0}},{{&l_197,&l_107[5],&l_107[5],&l_193},{&l_107[4],&l_107[4],&l_197,&l_197},{&l_193,&l_193,&l_193,&l_107[0]},{&l_162,&g_8,&l_107[5],(void*)0},{&l_107[5],&l_162,&l_107[5],&l_193},{&l_193,&g_8,&l_193,&l_174},{&l_193,&l_197,&l_174,&g_8},{(void*)0,&l_193,&l_193,&g_8},{&l_107[5],(void*)0,&l_107[5],(void*)0},{&l_197,&l_193,&l_107[4],&l_197}}}; + int i, j, k; + if (((((((*l_186) = l_184) == &g_67) && (safe_rshift_func_uint8_t_u_s(g_79, 6))) <= ((safe_mul_func_int16_t_s_s(p_46, (**l_84))) | (safe_rshift_func_uint16_t_u_s((&g_113 != &l_59), 14)))) <= g_60.f2)) + { /* block id: 103 */ + struct S2 **l_194 = (void*)0; + struct S2 *l_196 = &g_70; + struct S2 **l_195 = &l_196; + l_193 ^= (**g_124); + (*l_195) = &g_70; + (*g_124) = ((*l_84) = &l_162); + if (l_197) + break; + } + else + { /* block id: 109 */ + if (l_197) + break; + (*g_124) = &l_162; + } + g_203[3]++; + } + if (p_47) + { /* block id: 115 */ + if ((**l_84)) + break; + } + else + { /* block id: 117 */ + int32_t *l_207 = &l_193; + int32_t *l_208 = &l_193; + int32_t *l_209 = &l_193; + int32_t *l_210 = &l_107[5]; + int32_t *l_211 = &l_162; + int32_t *l_212 = &l_107[1]; + int32_t *l_213[6][4] = {{(void*)0,&l_107[2],(void*)0,&l_107[2]},{(void*)0,&l_107[2],(void*)0,&l_107[2]},{(void*)0,&l_107[2],(void*)0,&l_107[2]},{(void*)0,&l_107[2],(void*)0,&l_107[2]},{(void*)0,&l_107[2],(void*)0,&l_107[2]},{(void*)0,&l_107[2],(void*)0,&l_107[2]}}; + int i, j; + l_206 |= (l_174 = (**g_124)); + g_217[1]--; + } + } + for (l_168 = 0; (l_168 <= 15); l_168++) + { /* block id: 125 */ + uint16_t l_227 = 65535UL; + for (g_141 = 25; (g_141 < 10); --g_141) + { /* block id: 128 */ + uint32_t l_224[10] = {0x72F24C6DL,0x72F24C6DL,0x72F24C6DL,0x72F24C6DL,0x72F24C6DL,0x72F24C6DL,0x72F24C6DL,0x72F24C6DL,0x72F24C6DL,0x72F24C6DL}; + int i; + --l_224[8]; + l_227 = (-9L); + } + l_174 |= (*g_125); + } + } + for (g_70.f1 = 6; (g_70.f1 == 4); --g_70.f1) + { /* block id: 137 */ + int32_t *l_230 = &l_206; + int32_t *l_231 = &l_215; + int32_t *l_232 = (void*)0; + int32_t *l_233[1]; + struct S1 l_265 = {0UL,164}; + int i; + for (i = 0; i < 1; i++) + l_233[i] = &l_174; + g_235--; + for (l_215 = 0; (l_215 <= (-16)); l_215--) + { /* block id: 141 */ + struct S1 l_248[1][4][6] = {{{{9UL,284},{254UL,195},{9UL,284},{255UL,160},{9UL,284},{0x5BL,115}},{{0x72L,235},{0x79L,274},{255UL,160},{0x5BL,115},{0x5BL,115},{255UL,160}},{{254UL,195},{254UL,195},{254UL,155},{254UL,195},{0x79L,274},{254UL,195}},{{254UL,155},{254UL,155},{255UL,160},{0x79L,274},{0x72L,235},{0x72L,235}}}}; + int32_t l_251 = 0x6C6B0ADAL; + int i, j, k; + for (g_70.f5 = 6; (g_70.f5 != 46); ++g_70.f5) + { /* block id: 144 */ + int32_t *l_242 = (void*)0; + int32_t l_253[1][5][6] = {{{0xBA40BAC5L,0x923D8348L,0L,0x16EABE1CL,0x923D8348L,0x923D8348L},{0x923D8348L,0xB0590F93L,0x16EABE1CL,0xC21EBF77L,0L,0x6348B1DDL},{0x6348B1DDL,0xBA40BAC5L,0x923D8348L,0L,0x16EABE1CL,0x923D8348L},{0xC21EBF77L,0L,0xC21EBF77L,0xB0590F93L,0x6348B1DDL,0xBA40BAC5L},{0x6348B1DDL,0x923D8348L,0xB0590F93L,0xBA40BAC5L,0xBA40BAC5L,0xC21EBF77L}}}; + int i, j, k; + for (l_105 = 0; (l_105 <= 9); l_105 += 1) + { /* block id: 147 */ + int i; + (*g_124) = l_242; + (*l_230) ^= (safe_mul_func_int16_t_s_s((g_203[l_105] || 0L), (g_70.f5 < (p_46 == (safe_rshift_func_int8_t_s_s(p_46, 1)))))); + } + for (g_234 = 0; (g_234 <= 3); g_234 += 1) + { /* block id: 153 */ + l_248[0][1][0] = l_247[5][5][0]; + } + if ((safe_lshift_func_uint8_t_u_u(p_47, 4))) + { /* block id: 156 */ + struct S0 ***l_260 = &l_82; + g_255--; + l_174 &= (&g_113 != ((*l_260) = g_258)); + } + else + { /* block id: 160 */ + uint32_t l_261 = 0xCEF78231L; + ++l_261; + return g_156[1][6][3]; + } + } + (*l_230) &= l_251; + if (p_46) + break; + if ((l_251 = (l_174 |= ((*l_230) = (g_70.f7.f1 <= p_46))))) + { /* block id: 170 */ + const struct S1 l_264 = {250UL,40}; + l_265 = l_264; + } + else + { /* block id: 172 */ + uint16_t l_269 = 0xB5A1L; + for (g_252 = 0; (g_252 <= 7); ++g_252) + { /* block id: 175 */ + if (l_105) + goto lbl_268; + if (l_269) + continue; + } + } + } + } + l_215 = (((safe_mod_func_int32_t_s_s(((l_272 >= (safe_mul_func_uint16_t_u_u((safe_div_func_int8_t_s_s((((l_168 | (l_277 != (*g_258))) ^ g_70.f2.f2) < (safe_add_func_int16_t_s_s((l_280 , ((p_46 <= ((((g_281 ^ ((void*)0 == &l_280)) , 0xF0L) != g_8) <= p_47)) < 1L)), p_46))), p_46)), (-7L)))) < l_280.f2.f0), p_47)) ^ 4294967290UL) & 1UL); + } + return g_156[2][1][7]; +} + + +/* ------------------------------------------ */ +/* + * reads : g_54 g_60.f0 g_70 g_8 g_79 + * writes: g_67 g_79 + */ +static struct S0 * func_55(int16_t p_56, struct S0 * p_57) +{ /* block id: 4 */ + uint8_t *l_65 = (void*)0; + uint8_t *l_66 = &g_67; + int32_t l_73 = (-6L); + int32_t l_74 = 0x4EE4B4BCL; + int32_t l_75 = 1L; + int32_t *l_76 = &l_75; + int32_t *l_77 = &l_75; + int32_t *l_78[9][4] = {{&l_75,&l_73,&l_75,&l_75},{(void*)0,&l_75,&g_8,&g_8},{(void*)0,&l_75,(void*)0,&g_8},{&l_75,&l_73,&l_73,&g_8},{&g_8,&l_75,&g_8,&g_8},{&l_73,&l_73,&l_73,&g_8},{&l_75,&g_8,&l_75,&l_73},{&g_8,(void*)0,&l_75,(void*)0},{&l_75,&l_75,&g_8,&g_8}}; + int i, j; + l_75 |= (((safe_sub_func_uint8_t_u_u(g_54[7][8], (safe_mod_func_uint8_t_u_u(((*l_66) = g_60.f0), (safe_mul_func_int8_t_s_s((p_56 , (((g_70 , (g_54[5][5] && (0L | g_70.f8))) , (safe_lshift_func_int8_t_s_s((l_73 >= (((l_74 = g_70.f6.f0) > l_73) >= l_73)), 4))) < l_73)), 5UL)))))) , g_8) & l_73); + --g_79; + return p_57; +} + + + + +/* ---------------------------------------- */ +int main (int argc, char* argv[]) +{ + int i, j, k; + int print_hash_value = 0; + if (argc == 2 && strcmp(argv[1], "1") == 0) print_hash_value = 1; + platform_main_begin(); + crc32_gentab(); + func_1(); + transparent_crc(g_8, "g_8", print_hash_value); + for (i = 0; i < 10; i++) + { + for (j = 0; j < 9; j++) + { + transparent_crc(g_54[i][j], "g_54[i][j]", print_hash_value); + if (print_hash_value) printf("index = [%d][%d]\n", i, j); + + } + } + transparent_crc(g_60.f0, "g_60.f0", print_hash_value); + transparent_crc(g_60.f1, "g_60.f1", print_hash_value); + transparent_crc(g_60.f2, "g_60.f2", print_hash_value); + transparent_crc(g_67, "g_67", print_hash_value); + transparent_crc(g_70.f0.f0, "g_70.f0.f0", print_hash_value); + transparent_crc(g_70.f0.f1, "g_70.f0.f1", print_hash_value); + transparent_crc(g_70.f0.f2, "g_70.f0.f2", print_hash_value); + transparent_crc(g_70.f1, "g_70.f1", print_hash_value); + transparent_crc(g_70.f2.f0, "g_70.f2.f0", print_hash_value); + transparent_crc(g_70.f2.f1, "g_70.f2.f1", print_hash_value); + transparent_crc(g_70.f2.f2, "g_70.f2.f2", print_hash_value); + transparent_crc(g_70.f3, "g_70.f3", print_hash_value); + transparent_crc(g_70.f4, "g_70.f4", print_hash_value); + transparent_crc(g_70.f5, "g_70.f5", print_hash_value); + transparent_crc(g_70.f6.f0, "g_70.f6.f0", print_hash_value); + transparent_crc(g_70.f6.f1, "g_70.f6.f1", print_hash_value); + transparent_crc(g_70.f7.f0, "g_70.f7.f0", print_hash_value); + transparent_crc(g_70.f7.f1, "g_70.f7.f1", print_hash_value); + transparent_crc(g_70.f7.f2, "g_70.f7.f2", print_hash_value); + transparent_crc(g_70.f8, "g_70.f8", print_hash_value); + transparent_crc(g_79, "g_79", print_hash_value); + transparent_crc(g_141, "g_141", print_hash_value); + for (i = 0; i < 10; i++) + { + transparent_crc(g_203[i], "g_203[i]", print_hash_value); + if (print_hash_value) printf("index = [%d]\n", i); + + } + for (i = 0; i < 5; i++) + { + transparent_crc(g_217[i], "g_217[i]", print_hash_value); + if (print_hash_value) printf("index = [%d]\n", i); + + } + transparent_crc(g_234, "g_234", print_hash_value); + transparent_crc(g_235, "g_235", print_hash_value); + transparent_crc(g_252, "g_252", print_hash_value); + for (i = 0; i < 10; i++) + { + transparent_crc(g_254[i], "g_254[i]", print_hash_value); + if (print_hash_value) printf("index = [%d]\n", i); + + } + transparent_crc(g_255, "g_255", print_hash_value); + transparent_crc(g_281, "g_281", print_hash_value); + for (i = 0; i < 7; i++) + { + transparent_crc(g_285[i], "g_285[i]", print_hash_value); + if (print_hash_value) printf("index = [%d]\n", i); + + } + transparent_crc(g_304.f0, "g_304.f0", print_hash_value); + transparent_crc(g_304.f2, "g_304.f2", print_hash_value); + transparent_crc(g_304.f3, "g_304.f3", print_hash_value); + transparent_crc(g_351, "g_351", print_hash_value); + transparent_crc(g_361.f0, "g_361.f0", print_hash_value); + transparent_crc(g_361.f1, "g_361.f1", print_hash_value); + for (i = 0; i < 6; i++) + { + transparent_crc(g_371[i], "g_371[i]", print_hash_value); + if (print_hash_value) printf("index = [%d]\n", i); + + } + for (i = 0; i < 7; i++) + { + transparent_crc(g_389[i], "g_389[i]", print_hash_value); + if (print_hash_value) printf("index = [%d]\n", i); + + } + for (i = 0; i < 9; i++) + { + for (j = 0; j < 9; j++) + { + transparent_crc(g_397[i][j], "g_397[i][j]", print_hash_value); + if (print_hash_value) printf("index = [%d][%d]\n", i, j); + + } + } + transparent_crc(g_449, "g_449", print_hash_value); + transparent_crc(g_524.f0, "g_524.f0", print_hash_value); + transparent_crc(g_524.f2, "g_524.f2", print_hash_value); + transparent_crc(g_524.f3, "g_524.f3", print_hash_value); + for (i = 0; i < 10; i++) + { + for (j = 0; j < 2; j++) + { + for (k = 0; k < 6; k++) + { + transparent_crc(g_621[i][j][k], "g_621[i][j][k]", print_hash_value); + if (print_hash_value) printf("index = [%d][%d][%d]\n", i, j, k); + + } + } + } + for (i = 0; i < 6; i++) + { + transparent_crc(g_739[i], "g_739[i]", print_hash_value); + if (print_hash_value) printf("index = [%d]\n", i); + + } + transparent_crc(g_745, "g_745", print_hash_value); + transparent_crc(g_747, "g_747", print_hash_value); + for (i = 0; i < 2; i++) + { + transparent_crc(g_829[i], "g_829[i]", print_hash_value); + if (print_hash_value) printf("index = [%d]\n", i); + + } + for (i = 0; i < 9; i++) + { + for (j = 0; j < 5; j++) + { + for (k = 0; k < 1; k++) + { + transparent_crc(g_864[i][j][k], "g_864[i][j][k]", print_hash_value); + if (print_hash_value) printf("index = [%d][%d][%d]\n", i, j, k); + + } + } + } + for (i = 0; i < 10; i++) + { + transparent_crc(g_873[i].f0.f0, "g_873[i].f0.f0", print_hash_value); + transparent_crc(g_873[i].f0.f1, "g_873[i].f0.f1", print_hash_value); + transparent_crc(g_873[i].f0.f2, "g_873[i].f0.f2", print_hash_value); + transparent_crc(g_873[i].f1, "g_873[i].f1", print_hash_value); + transparent_crc(g_873[i].f2.f0, "g_873[i].f2.f0", print_hash_value); + transparent_crc(g_873[i].f2.f1, "g_873[i].f2.f1", print_hash_value); + transparent_crc(g_873[i].f2.f2, "g_873[i].f2.f2", print_hash_value); + transparent_crc(g_873[i].f3, "g_873[i].f3", print_hash_value); + transparent_crc(g_873[i].f4, "g_873[i].f4", print_hash_value); + transparent_crc(g_873[i].f5, "g_873[i].f5", print_hash_value); + transparent_crc(g_873[i].f6.f0, "g_873[i].f6.f0", print_hash_value); + transparent_crc(g_873[i].f6.f1, "g_873[i].f6.f1", print_hash_value); + transparent_crc(g_873[i].f7.f0, "g_873[i].f7.f0", print_hash_value); + transparent_crc(g_873[i].f7.f1, "g_873[i].f7.f1", print_hash_value); + transparent_crc(g_873[i].f7.f2, "g_873[i].f7.f2", print_hash_value); + transparent_crc(g_873[i].f8, "g_873[i].f8", print_hash_value); + if (print_hash_value) printf("index = [%d]\n", i); + + } + transparent_crc(g_921, "g_921", print_hash_value); + transparent_crc(g_973, "g_973", print_hash_value); + for (i = 0; i < 1; i++) + { + for (j = 0; j < 5; j++) + { + for (k = 0; k < 1; k++) + { + transparent_crc(g_987[i][j][k], "g_987[i][j][k]", print_hash_value); + if (print_hash_value) printf("index = [%d][%d][%d]\n", i, j, k); + + } + } + } + transparent_crc(g_994.f0.f0, "g_994.f0.f0", print_hash_value); + transparent_crc(g_994.f0.f1, "g_994.f0.f1", print_hash_value); + transparent_crc(g_994.f0.f2, "g_994.f0.f2", print_hash_value); + transparent_crc(g_994.f1, "g_994.f1", print_hash_value); + transparent_crc(g_994.f2.f0, "g_994.f2.f0", print_hash_value); + transparent_crc(g_994.f2.f1, "g_994.f2.f1", print_hash_value); + transparent_crc(g_994.f2.f2, "g_994.f2.f2", print_hash_value); + transparent_crc(g_994.f3, "g_994.f3", print_hash_value); + transparent_crc(g_994.f4, "g_994.f4", print_hash_value); + transparent_crc(g_994.f5, "g_994.f5", print_hash_value); + transparent_crc(g_994.f6.f0, "g_994.f6.f0", print_hash_value); + transparent_crc(g_994.f6.f1, "g_994.f6.f1", print_hash_value); + transparent_crc(g_994.f7.f0, "g_994.f7.f0", print_hash_value); + transparent_crc(g_994.f7.f1, "g_994.f7.f1", print_hash_value); + transparent_crc(g_994.f7.f2, "g_994.f7.f2", print_hash_value); + transparent_crc(g_994.f8, "g_994.f8", print_hash_value); + for (i = 0; i < 4; i++) + { + transparent_crc(g_1120[i], "g_1120[i]", print_hash_value); + if (print_hash_value) printf("index = [%d]\n", i); + + } + transparent_crc(g_1191, "g_1191", print_hash_value); + transparent_crc(g_1371, "g_1371", print_hash_value); + transparent_crc(g_1439.f0.f0, "g_1439.f0.f0", print_hash_value); + transparent_crc(g_1439.f0.f1, "g_1439.f0.f1", print_hash_value); + transparent_crc(g_1439.f0.f2, "g_1439.f0.f2", print_hash_value); + transparent_crc(g_1439.f1, "g_1439.f1", print_hash_value); + transparent_crc(g_1439.f2.f0, "g_1439.f2.f0", print_hash_value); + transparent_crc(g_1439.f2.f1, "g_1439.f2.f1", print_hash_value); + transparent_crc(g_1439.f2.f2, "g_1439.f2.f2", print_hash_value); + transparent_crc(g_1439.f3, "g_1439.f3", print_hash_value); + transparent_crc(g_1439.f4, "g_1439.f4", print_hash_value); + transparent_crc(g_1439.f5, "g_1439.f5", print_hash_value); + transparent_crc(g_1439.f6.f0, "g_1439.f6.f0", print_hash_value); + transparent_crc(g_1439.f6.f1, "g_1439.f6.f1", print_hash_value); + transparent_crc(g_1439.f7.f0, "g_1439.f7.f0", print_hash_value); + transparent_crc(g_1439.f7.f1, "g_1439.f7.f1", print_hash_value); + transparent_crc(g_1439.f7.f2, "g_1439.f7.f2", print_hash_value); + transparent_crc(g_1439.f8, "g_1439.f8", print_hash_value); + transparent_crc(g_1441, "g_1441", print_hash_value); + transparent_crc(g_1672.f0.f0, "g_1672.f0.f0", print_hash_value); + transparent_crc(g_1672.f0.f1, "g_1672.f0.f1", print_hash_value); + transparent_crc(g_1672.f0.f2, "g_1672.f0.f2", print_hash_value); + transparent_crc(g_1672.f1, "g_1672.f1", print_hash_value); + transparent_crc(g_1672.f2.f0, "g_1672.f2.f0", print_hash_value); + transparent_crc(g_1672.f2.f1, "g_1672.f2.f1", print_hash_value); + transparent_crc(g_1672.f2.f2, "g_1672.f2.f2", print_hash_value); + transparent_crc(g_1672.f3, "g_1672.f3", print_hash_value); + transparent_crc(g_1672.f4, "g_1672.f4", print_hash_value); + transparent_crc(g_1672.f5, "g_1672.f5", print_hash_value); + transparent_crc(g_1672.f6.f0, "g_1672.f6.f0", print_hash_value); + transparent_crc(g_1672.f6.f1, "g_1672.f6.f1", print_hash_value); + transparent_crc(g_1672.f7.f0, "g_1672.f7.f0", print_hash_value); + transparent_crc(g_1672.f7.f1, "g_1672.f7.f1", print_hash_value); + transparent_crc(g_1672.f7.f2, "g_1672.f7.f2", print_hash_value); + transparent_crc(g_1672.f8, "g_1672.f8", print_hash_value); + platform_main_end(crc32_context ^ 0xFFFFFFFFUL, print_hash_value); + return 0; +} + +/************************ statistics ************************* +XXX max struct depth: 2 +breakdown: + depth: 0, occurrence: 514 + depth: 1, occurrence: 51 + depth: 2, occurrence: 8 +XXX total union variables: 51 + +XXX non-zero bitfields defined in structs: 5 +XXX zero bitfields defined in structs: 0 +XXX const bitfields defined in structs: 1 +XXX volatile bitfields defined in structs: 0 +XXX structs with bitfields in the program: 167 +breakdown: + indirect level: 0, occurrence: 110 + indirect level: 1, occurrence: 28 + indirect level: 2, occurrence: 17 + indirect level: 3, occurrence: 6 + indirect level: 4, occurrence: 6 +XXX full-bitfields structs in the program: 22 +breakdown: + indirect level: 0, occurrence: 22 +XXX times a bitfields struct's address is taken: 128 +XXX times a bitfields struct on LHS: 26 +XXX times a bitfields struct on RHS: 257 +XXX times a single bitfield on LHS: 22 +XXX times a single bitfield on RHS: 141 + +XXX max expression depth: 38 +breakdown: + depth: 1, occurrence: 395 + depth: 2, occurrence: 99 + depth: 3, occurrence: 7 + depth: 4, occurrence: 7 + depth: 5, occurrence: 6 + depth: 6, occurrence: 3 + depth: 7, occurrence: 2 + depth: 8, occurrence: 3 + depth: 11, occurrence: 4 + depth: 12, occurrence: 3 + depth: 13, occurrence: 1 + depth: 14, occurrence: 2 + depth: 15, occurrence: 5 + depth: 16, occurrence: 3 + depth: 17, occurrence: 5 + depth: 18, occurrence: 2 + depth: 19, occurrence: 3 + depth: 20, occurrence: 2 + depth: 21, occurrence: 2 + depth: 23, occurrence: 4 + depth: 24, occurrence: 1 + depth: 25, occurrence: 3 + depth: 26, occurrence: 1 + depth: 27, occurrence: 2 + depth: 30, occurrence: 3 + depth: 37, occurrence: 1 + depth: 38, occurrence: 1 + +XXX total number of pointers: 473 + +XXX times a variable address is taken: 1070 +XXX times a pointer is dereferenced on RHS: 207 +breakdown: + depth: 1, occurrence: 164 + depth: 2, occurrence: 36 + depth: 3, occurrence: 7 +XXX times a pointer is dereferenced on LHS: 256 +breakdown: + depth: 1, occurrence: 246 + depth: 2, occurrence: 10 +XXX times a pointer is compared with null: 24 +XXX times a pointer is compared with address of another variable: 4 +XXX times a pointer is compared with another pointer: 11 +XXX times a pointer is qualified to be dereferenced: 6567 + +XXX max dereference level: 4 +breakdown: + level: 0, occurrence: 0 + level: 1, occurrence: 1418 + level: 2, occurrence: 286 + level: 3, occurrence: 49 + level: 4, occurrence: 7 +XXX number of pointers point to pointers: 137 +XXX number of pointers point to scalars: 260 +XXX number of pointers point to structs: 53 +XXX percent of pointers has null in alias set: 27.5 +XXX average alias set size: 1.4 + +XXX times a non-volatile is read: 1744 +XXX times a non-volatile is write: 819 +XXX times a volatile is read: 0 +XXX times read thru a pointer: 0 +XXX times a volatile is write: 0 +XXX times written thru a pointer: 0 +XXX times a volatile is available for access: 0 +XXX percentage of non-volatile access: 100 + +XXX forward jumps: 0 +XXX backward jumps: 5 + +XXX stmts: 369 +XXX max block depth: 5 +breakdown: + depth: 0, occurrence: 33 + depth: 1, occurrence: 33 + depth: 2, occurrence: 52 + depth: 3, occurrence: 52 + depth: 4, occurrence: 88 + depth: 5, occurrence: 111 + +XXX percentage a fresh-made variable is used: 18.1 +XXX percentage an existing variable is used: 81.9 +FYI: the random generator makes assumptions about the integer size. See platform.info for more details. +********************* end of statistics **********************/ + diff --git a/tests/fuzz/8.c.txt b/tests/fuzz/8.c.txt new file mode 100644 index 00000000..02f9ff4a --- /dev/null +++ b/tests/fuzz/8.c.txt @@ -0,0 +1 @@ +checksum = 38B8E180 diff --git a/tests/fuzz/creduce_tester.py b/tests/fuzz/creduce_tester.py new file mode 100755 index 00000000..c3460e9d --- /dev/null +++ b/tests/fuzz/creduce_tester.py @@ -0,0 +1,53 @@ +#!/usr/bin/python + +''' +Runs csmith, a C fuzzer, and looks for bugs +''' + +import os, sys, difflib +from subprocess import Popen, PIPE, STDOUT + +sys.path += [os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'tools')] +import shared + +filename = sys.argv[1] +print 'testing file', filename + +print '2) Compile natively' +shared.try_delete(filename) +shared.execute([shared.CLANG_CC, '-O2', filename + '.c', '-o', filename] + CSMITH_CFLAGS, stderr=PIPE) +assert os.path.exists(filename) +print '3) Run natively' +try: + correct = shared.timeout_run(Popen([filename], stdout=PIPE, stderr=PIPE), 3) +except Exception, e: + print 'Failed or infinite looping in native, skipping', e + notes['invalid'] += 1 + os.exit(0) # boring + +print '4) Compile JS-ly and compare' + +def try_js(args): + shared.try_delete(filename + '.js') + shared.execute([shared.EMCC, '-O2', '-s', 'ASM_JS=1', '-s', 'PRECISE_I64_MATH=1', '-s', 'PRECISE_I32_MUL=1', filename + '.c', '-o', filename + '.js'] + CSMITH_CFLAGS + args, stderr=PIPE) + assert os.path.exists(filename + '.js') + js = shared.run_js(filename + '.js', stderr=PIPE, engine=engine1) + assert correct == js, ''.join([a.rstrip()+'\n' for a in difflib.unified_diff(correct.split('\n'), js.split('\n'), fromfile='expected', tofile='actual')]) + +# Try normally, then try unaligned because csmith does generate nonportable code that requires x86 alignment +ok = False +normal = True +for args, note in [([], None), (['-s', 'UNALIGNED_MEMORY=1'], 'unaligned')]: + try: + try_js(args) + ok = True + if note: + notes[note] += 1 + break + except Exception, e: + print e + normal = False +if not ok: sys.exit(1) + +sys.exit(0) # boring + diff --git a/tests/fuzz/csmith.h b/tests/fuzz/csmith.h new file mode 100644 index 00000000..f1334a37 --- /dev/null +++ b/tests/fuzz/csmith.h @@ -0,0 +1,130 @@ +/* -*- mode: C -*- + * + * Copyright (c) 2007-2010 The University of Utah + * All rights reserved. + * + * This file is part of `csmith', a random generator of C programs. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef RANDOM_RUNTIME_H +#define RANDOM_RUNTIME_H + +#ifdef CSMITH_MINIMAL +#include "csmith_minimal.h" +#else + +/*****************************************************************************/ + +#include <string.h> + +#define __STDC_LIMIT_MACROS +#include "random_inc.h" + +static uint32_t crc32_tab[256]; +static uint32_t crc32_context = 0xFFFFFFFFUL; + +static void +crc32_gentab (void) +{ + uint32_t crc; + const uint32_t poly = 0xEDB88320UL; + int i, j; + + for (i = 0; i < 256; i++) { + crc = i; + for (j = 8; j > 0; j--) { + if (crc & 1) { + crc = (crc >> 1) ^ poly; + } else { + crc >>= 1; + } + } + crc32_tab[i] = crc; + } +} + +static void +crc32_byte (uint8_t b) { + crc32_context = + ((crc32_context >> 8) & 0x00FFFFFF) ^ + crc32_tab[(crc32_context ^ b) & 0xFF]; +} + +#if defined(__SPLAT__) || defined (__COMPCERT__) || defined(NO_LONGLONG) +static void +crc32_8bytes (uint32_t val) +{ + crc32_byte ((val>>0) & 0xff); + crc32_byte ((val>>8) & 0xff); + crc32_byte ((val>>16) & 0xff); + crc32_byte ((val>>24) & 0xff); +} + +static void +transparent_crc (uint32_t val, char* vname, int flag) +{ + crc32_8bytes(val); + if (flag) { + printf("...checksum after hashing %s : %X\n", vname, crc32_context ^ 0xFFFFFFFFU); + } +} +#else +static void +crc32_8bytes (uint64_t val) +{ + crc32_byte ((val>>0) & 0xff); + crc32_byte ((val>>8) & 0xff); + crc32_byte ((val>>16) & 0xff); + crc32_byte ((val>>24) & 0xff); + crc32_byte ((val>>32) & 0xff); + crc32_byte ((val>>40) & 0xff); + crc32_byte ((val>>48) & 0xff); + crc32_byte ((val>>56) & 0xff); +} + +static void +transparent_crc (uint64_t val, char* vname, int flag) +{ + crc32_8bytes(val); + if (flag) { + printf("...checksum after hashing %s : %lX\n", vname, crc32_context ^ 0xFFFFFFFFUL); + } +} +#endif + +/*****************************************************************************/ + +#endif + +#endif /* RANDOM_RUNTIME_H */ + +/* + * Local Variables: + * c-basic-offset: 4 + * tab-width: 4 + * End: + */ + +/* End of file. */ diff --git a/tests/fuzz/csmith_driver.py b/tests/fuzz/csmith_driver.py new file mode 100755 index 00000000..5bdef009 --- /dev/null +++ b/tests/fuzz/csmith_driver.py @@ -0,0 +1,122 @@ +#!/usr/bin/python + +''' +Runs csmith, a C fuzzer, and looks for bugs +''' + +import os, sys, difflib, shutil +from subprocess import Popen, PIPE, STDOUT + +sys.path += [os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'tools')] +import shared + +engine1 = eval('shared.' + sys.argv[1]) if len(sys.argv) > 1 else shared.JS_ENGINES[0] +engine2 = eval('shared.' + sys.argv[2]) if len(sys.argv) > 2 else None + +print 'testing js engines', engine1, engine2 + +CSMITH = os.path.expanduser('~/Dev/csmith/src/csmith') +CSMITH_CFLAGS = ['-I' + os.path.expanduser('~/Dev/csmith/runtime/')] + +filename = os.path.join(shared.CANONICAL_TEMP_DIR, 'fuzzcode') + +shared.DEFAULT_TIMEOUT = 1 + +tried = 0 + +notes = { 'invalid': 0, 'unaligned': 0, 'embug': 0 } + +fails = 0 + +while 1: + print 'Tried %d, notes: %s' % (tried, notes) + print '1) Generate C' + shared.execute([CSMITH, '--no-volatiles', '--no-math64', '--no-packed-struct'],# + + #['--max-block-depth', '2', '--max-block-size', '2', '--max-expr-complexity', '2', '--max-funcs', '2'], + stdout=open(filename + '.c', 'w')) + #shutil.copyfile(filename + '.c', 'testcase%d.c' % tried) + print '1) Generate C... %.2f K of C source' % (len(open(filename + '.c').read())/1024.) + + tried += 1 + + print '2) Compile natively' + shared.try_delete(filename) + shared.execute([shared.CLANG_CC, '-O2', filename + '.c', '-o', filename + '1'] + CSMITH_CFLAGS, stderr=PIPE) # + shared.EMSDK_OPTS + shared.execute([shared.CLANG_CC, '-O2', '-emit-llvm', '-c', '-Xclang', '-triple=i386-pc-linux-gnu', filename + '.c', '-o', filename + '.bc'] + CSMITH_CFLAGS + shared.EMSDK_OPTS, stderr=PIPE) + shared.execute([shared.path_from_root('tools', 'nativize_llvm.py'), filename + '.bc'], stdout=PIPE, stderr=PIPE) + shutil.move(filename + '.bc.run', filename + '2') + shared.execute([shared.CLANG_CC, filename + '.c', '-o', filename + '3'] + CSMITH_CFLAGS, stderr=PIPE) + print '3) Run natively' + try: + correct1 = shared.jsrun.timeout_run(Popen([filename + '1'], stdout=PIPE, stderr=PIPE), 3) + if 'Segmentation fault' in correct1 or len(correct1) < 10: raise Exception('segfault') + correct2 = shared.jsrun.timeout_run(Popen([filename + '2'], stdout=PIPE, stderr=PIPE), 3) + if 'Segmentation fault' in correct2 or len(correct2) < 10: raise Exception('segfault') + correct3 = shared.jsrun.timeout_run(Popen([filename + '3'], stdout=PIPE, stderr=PIPE), 3) + if 'Segmentation fault' in correct3 or len(correct3) < 10: raise Exception('segfault') + if correct1 != correct3: raise Exception('clang opts change result') + except Exception, e: + print 'Failed or infinite looping in native, skipping', e + notes['invalid'] += 1 + continue + + print '4) Compile JS-ly and compare' + + def try_js(args): + shared.try_delete(filename + '.js') + print '(compile)' + shared.execute([shared.EMCC, '-O2', '-s', 'ASM_JS=1', filename + '.c', '-o', filename + '.js'] + CSMITH_CFLAGS + args, stderr=PIPE) + assert os.path.exists(filename + '.js') + print '(run)' + js = shared.run_js(filename + '.js', stderr=PIPE, engine=engine1, check_timeout=True) + assert correct1 == js or correct2 == js, ''.join([a.rstrip()+'\n' for a in difflib.unified_diff(correct1.split('\n'), js.split('\n'), fromfile='expected', tofile='actual')]) + + # Try normally, then try unaligned because csmith does generate nonportable code that requires x86 alignment + ok = False + normal = True + for args, note in [([], None), (['-s', 'UNALIGNED_MEMORY=1'], 'unaligned')]: + try: + try_js(args) + ok = True + if note: + notes[note] += 1 + break + except Exception, e: + print e + normal = False + #open('testcase%d.js' % tried, 'w').write( + # open(filename + '.js').read().replace(' var ret = run();', ' var ret = run(["1"]);') + #) + if not ok: + print "EMSCRIPTEN BUG" + notes['embug'] += 1 + fails += 1 + shutil.copyfile('fuzzcode.c', 'newfail%d.c' % fails) + continue + #if not ok: + # try: # finally, try with safe heap. if that is triggered, this is nonportable code almost certainly + # try_js(['-s', 'SAFE_HEAP=1']) + # except Exception, e: + # print e + # js = shared.run_js(filename + '.js', stderr=PIPE, full_output=True) + # print js + # if 'SAFE_HEAP' in js: + # notes['safeheap'] += 1 + # else: + # break + + # This is ok. Try in secondary JS engine too + if engine2 and normal: + try: + js2 = shared.run_js(filename + '.js', stderr=PIPE, engine=engine2, full_output=True, check_timeout=True) + except: + print 'failed to run in secondary', js2 + break + + # asm.js testing + assert 'warning: Successfully compiled asm.js code' in js2, 'must validate' + js2 = js2.replace('\nwarning: Successfully compiled asm.js code\n', '') + + assert js2 == correct1 or js2 == correct2, ''.join([a.rstrip()+'\n' for a in difflib.unified_diff(correct1.split('\n'), js2.split('\n'), fromfile='expected', tofile='actual')]) + 'ODIN FAIL' + print 'odin ok' + diff --git a/tests/fuzz/platform_generic.h b/tests/fuzz/platform_generic.h new file mode 100644 index 00000000..b2ef33a3 --- /dev/null +++ b/tests/fuzz/platform_generic.h @@ -0,0 +1,132 @@ +/* -*- mode: C -*- + * + * + * Copyright (c) 2007, 2008 The University of Utah + * All rights reserved. + * + * This file is part of `csmith', a random generator of C programs. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef PLATFORM_GENERIC_H +#define PLATFORM_GENERIC_H + +/*****************************************************************************/ + +#ifdef STANDALONE +extern int printf (const char *, ...); +#else +#include <stdio.h> +#endif + +static void +platform_main_begin(void) +{ + /* Nothing to do. */ +} + +static void +platform_main_end(uint32_t crc, int flag) +{ +#if defined (__FRAMAC) + Frama_C_dump_assert_each(); +#endif + printf ("checksum = %X\n", crc); +#if defined (LOG_WRAPPERS) + { + int i, first; + + printf ("executed wrappers: "); + first = 1; + for (i=1; i<N_WRAP+1; i++) { + if (__executed_checks[i]) { + if (!first) { + printf (","); + } else { + first = 0; + } + printf ("%d", i); + } + } + printf ("\n"); + + printf ("dead wrappers: "); + first = 1; + for (i=1; i<N_WRAP+1; i++) { + if (!__executed_checks[i]) { + if (!first) { + printf (","); + } else { + first = 0; + } + printf ("%d", i); + } + } + printf ("\n"); + + printf ("wrappers that failed at least once: "); + first = 1; + for (i=1; i<N_WRAP+1; i++) { + if (__failed_checks[i]) { + if (!first) { + printf (","); + } else { + first = 0; + } + printf ("%d", i); + } + } + printf ("\n"); + + printf ("wrappers that never failed (or never executed): "); + first = 1; + for (i=1; i<N_WRAP+1; i++) { + if (!__failed_checks[i]) { + if (!first) { + printf (","); + } else { + first = 0; + } + printf ("%d", i); + } + } + printf ("\n"); + } +#endif +} + +#define MB (1<<20) + +/*****************************************************************************/ + +#endif /* PLATFORM_GENERIC_H */ + +/* + * Local Variables: + * c-basic-offset: 4 + * tab-width: 4 + * End: + */ + +/* End of file. */ diff --git a/tests/fuzz/random_inc.h b/tests/fuzz/random_inc.h new file mode 100644 index 00000000..7559cbd7 --- /dev/null +++ b/tests/fuzz/random_inc.h @@ -0,0 +1,129 @@ +/* -*- mode: C -*- + * + * Copyright (c) 2007-2010 The University of Utah + * All rights reserved. + * + * This file is part of `csmith', a random generator of C programs. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef RANDOM_INC_H +#define RANDOM_INC_H + + +#if defined(STANDALONE) + #if defined(_MSC_VER) + #include <limits.h> + #include "windows/stdint.h" + #elif defined (IA32_ARCH) + #include "stdint_ia32.h" + #elif defined (IA64_ARCH) + #include "stdint_ia64.h" + #elif defined (MSP430) + #include "stdint_msp430.h" + #elif defined (AVR_ARCH) + #include "stdint_avr.h" + #else + #include "stdint_ia32.h" + #endif +#else + #include <limits.h> + #if defined(_MSC_VER) + #include "windows/stdint.h" + #else + #include <stdint.h> + #endif +#endif + +#include <assert.h> + +/*****************************************************************************/ + +#ifndef DEPUTY +#define COUNT(n) +#define TC +#define SAFE +#endif + +/*****************************************************************************/ + +#ifdef LOG_WRAPPERS +#include "wrapper.h" +char __failed_checks[N_WRAP+1]; +char __executed_checks[N_WRAP+1]; +#define UNDEFINED(__val) (__failed_checks[index]=1,(__val)) +#define LOG_INDEX , int index +#define LOG_EXEC __executed_checks[index]=1; +#else +#define UNDEFINED(__val) (__val) +#define LOG_INDEX +#define LOG_EXEC +#endif + +#if defined(AVR_ARCH) +#include "platform_avr.h" +#elif defined (MSP430) +#include "platform_msp430.h" +#else +#include "platform_generic.h" +#endif + +#define STATIC static + +#if defined (USE_MATH_MACROS_NOTMP) +#include "safe_math_macros_notmp.h" +#elif defined (USE_MATH_MACROS) +#include "safe_math_macros.h" +#else +#define FUNC_NAME(x) (safe_##x) +#include "safe_math.h" +#undef FUNC_NAME +#endif + +#define INT_BIT (sizeof(int)*CHAR_BIT) +#define _CSMITH_BITFIELD(x) (((x)>INT_BIT)?((x)%INT_BIT):(x)) + +#ifdef TCC + +void* memcpy(void* dest, const void* src, size_t count) { + char* dst8 = (char*)dest; + char* src8 = (char*)src; + + while (count--) { + *dst8++ = *src8++; + } + return dest; +} + +void *memset(void *s, int c, size_t n) +{ + unsigned char* p=s; + while(n--) + *p++ = (unsigned char)c; + return s; +} + +#endif + +#endif // RANDOM_INC_H diff --git a/tests/fuzz/safe_math.h b/tests/fuzz/safe_math.h new file mode 100644 index 00000000..393ebba1 --- /dev/null +++ b/tests/fuzz/safe_math.h @@ -0,0 +1,947 @@ + +#ifndef SAFE_MATH_H +#define SAFE_MATH_H + + + + + + + + + +STATIC int8_t +FUNC_NAME(unary_minus_func_int8_t_s)(int8_t si LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE +#if (INT8_MAX>=INT_MAX) + (si==INT8_MIN) ? + (UNDEFINED(si)) : +#endif +#endif + -si; +} + +STATIC int8_t +FUNC_NAME(add_func_int8_t_s_s)(int8_t si1, int8_t si2 LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE +#if (INT8_MAX>=INT_MAX) + (((si1>0) && (si2>0) && (si1 > (INT8_MAX-si2))) || ((si1<0) && (si2<0) && (si1 < (INT8_MIN-si2)))) ? + (UNDEFINED(si1)) : +#endif +#endif + (si1 + si2); +} + +STATIC int8_t +FUNC_NAME(sub_func_int8_t_s_s)(int8_t si1, int8_t si2 LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE +#if (INT8_MAX>=INT_MAX) + (((si1^si2) & (((si1 ^ ((si1^si2) & (~INT8_MAX)))-si2)^si2)) < 0) ? + (UNDEFINED(si1)) : +#endif +#endif + (si1 - si2); +} + +STATIC int8_t +FUNC_NAME(mul_func_int8_t_s_s)(int8_t si1, int8_t si2 LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE +#if (INT8_MAX>=INT_MAX) + (((si1 > 0) && (si2 > 0) && (si1 > (INT8_MAX / si2))) || ((si1 > 0) && (si2 <= 0) && (si2 < (INT8_MIN / si1))) || ((si1 <= 0) && (si2 > 0) && (si1 < (INT8_MIN / si2))) || ((si1 <= 0) && (si2 <= 0) && (si1 != 0) && (si2 < (INT8_MAX / si1)))) ? + (UNDEFINED(si1)) : +#endif +#endif + si1 * si2; +} + +STATIC int8_t +FUNC_NAME(mod_func_int8_t_s_s)(int8_t si1, int8_t si2 LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + ((si2 == 0) || ((si1 == INT8_MIN) && (si2 == (-1)))) ? + (UNDEFINED(si1)) : +#endif + (si1 % si2); +} + +STATIC int8_t +FUNC_NAME(div_func_int8_t_s_s)(int8_t si1, int8_t si2 LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + ((si2 == 0) || ((si1 == INT8_MIN) && (si2 == (-1)))) ? + (UNDEFINED(si1)) : +#endif + (si1 / si2); +} + +STATIC int8_t +FUNC_NAME(lshift_func_int8_t_s_s)(int8_t left, int right LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + ((left < 0) || (((int)right) < 0) || (((int)right) >= 32) || (left > (INT8_MAX >> ((int)right)))) ? + (UNDEFINED(left)) : +#endif + (left << ((int)right)); +} + +STATIC int8_t +FUNC_NAME(lshift_func_int8_t_s_u)(int8_t left, unsigned int right LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + ((left < 0) || (((unsigned int)right) >= 32) || (left > (INT8_MAX >> ((unsigned int)right)))) ? + (UNDEFINED(left)) : +#endif + (left << ((unsigned int)right)); +} + +STATIC int8_t +FUNC_NAME(rshift_func_int8_t_s_s)(int8_t left, int right LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + ((left < 0) || (((int)right) < 0) || (((int)right) >= 32))? + (UNDEFINED(left)) : +#endif + (left >> ((int)right)); +} + +STATIC int8_t +FUNC_NAME(rshift_func_int8_t_s_u)(int8_t left, unsigned int right LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + ((left < 0) || (((unsigned int)right) >= 32)) ? + (UNDEFINED(left)) : +#endif + (left >> ((unsigned int)right)); +} + + + +STATIC int16_t +FUNC_NAME(unary_minus_func_int16_t_s)(int16_t si LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE +#if (INT16_MAX>=INT_MAX) + (si==INT16_MIN) ? + (UNDEFINED(si)) : +#endif +#endif + -si; +} + +STATIC int16_t +FUNC_NAME(add_func_int16_t_s_s)(int16_t si1, int16_t si2 LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE +#if (INT16_MAX>=INT_MAX) + (((si1>0) && (si2>0) && (si1 > (INT16_MAX-si2))) || ((si1<0) && (si2<0) && (si1 < (INT16_MIN-si2)))) ? + (UNDEFINED(si1)) : +#endif +#endif + (si1 + si2); +} + +STATIC int16_t +FUNC_NAME(sub_func_int16_t_s_s)(int16_t si1, int16_t si2 LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE +#if (INT16_MAX>=INT_MAX) + (((si1^si2) & (((si1 ^ ((si1^si2) & (~INT16_MAX)))-si2)^si2)) < 0) ? + (UNDEFINED(si1)) : +#endif +#endif + (si1 - si2); +} + +STATIC int16_t +FUNC_NAME(mul_func_int16_t_s_s)(int16_t si1, int16_t si2 LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE +#if (INT16_MAX>=INT_MAX) + (((si1 > 0) && (si2 > 0) && (si1 > (INT16_MAX / si2))) || ((si1 > 0) && (si2 <= 0) && (si2 < (INT16_MIN / si1))) || ((si1 <= 0) && (si2 > 0) && (si1 < (INT16_MIN / si2))) || ((si1 <= 0) && (si2 <= 0) && (si1 != 0) && (si2 < (INT16_MAX / si1)))) ? + (UNDEFINED(si1)) : +#endif +#endif + si1 * si2; +} + +STATIC int16_t +FUNC_NAME(mod_func_int16_t_s_s)(int16_t si1, int16_t si2 LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + ((si2 == 0) || ((si1 == INT16_MIN) && (si2 == (-1)))) ? + (UNDEFINED(si1)) : +#endif + (si1 % si2); +} + +STATIC int16_t +FUNC_NAME(div_func_int16_t_s_s)(int16_t si1, int16_t si2 LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + ((si2 == 0) || ((si1 == INT16_MIN) && (si2 == (-1)))) ? + (UNDEFINED(si1)) : +#endif + (si1 / si2); +} + +STATIC int16_t +FUNC_NAME(lshift_func_int16_t_s_s)(int16_t left, int right LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + ((left < 0) || (((int)right) < 0) || (((int)right) >= 32) || (left > (INT16_MAX >> ((int)right)))) ? + (UNDEFINED(left)) : +#endif + (left << ((int)right)); +} + +STATIC int16_t +FUNC_NAME(lshift_func_int16_t_s_u)(int16_t left, unsigned int right LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + ((left < 0) || (((unsigned int)right) >= 32) || (left > (INT16_MAX >> ((unsigned int)right)))) ? + (UNDEFINED(left)) : +#endif + (left << ((unsigned int)right)); +} + +STATIC int16_t +FUNC_NAME(rshift_func_int16_t_s_s)(int16_t left, int right LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + ((left < 0) || (((int)right) < 0) || (((int)right) >= 32))? + (UNDEFINED(left)) : +#endif + (left >> ((int)right)); +} + +STATIC int16_t +FUNC_NAME(rshift_func_int16_t_s_u)(int16_t left, unsigned int right LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + ((left < 0) || (((unsigned int)right) >= 32)) ? + (UNDEFINED(left)) : +#endif + (left >> ((unsigned int)right)); +} + + + +STATIC int32_t +FUNC_NAME(unary_minus_func_int32_t_s)(int32_t si LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE +#if (INT32_MAX>=INT_MAX) + (si==INT32_MIN) ? + (UNDEFINED(si)) : +#endif +#endif + -si; +} + +STATIC int32_t +FUNC_NAME(add_func_int32_t_s_s)(int32_t si1, int32_t si2 LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE +#if (INT32_MAX>=INT_MAX) + (((si1>0) && (si2>0) && (si1 > (INT32_MAX-si2))) || ((si1<0) && (si2<0) && (si1 < (INT32_MIN-si2)))) ? + (UNDEFINED(si1)) : +#endif +#endif + (si1 + si2); +} + +STATIC int32_t +FUNC_NAME(sub_func_int32_t_s_s)(int32_t si1, int32_t si2 LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE +#if (INT32_MAX>=INT_MAX) + (((si1^si2) & (((si1 ^ ((si1^si2) & (~INT32_MAX)))-si2)^si2)) < 0) ? + (UNDEFINED(si1)) : +#endif +#endif + (si1 - si2); +} + +STATIC int32_t +FUNC_NAME(mul_func_int32_t_s_s)(int32_t si1, int32_t si2 LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE +#if (INT32_MAX>=INT_MAX) + (((si1 > 0) && (si2 > 0) && (si1 > (INT32_MAX / si2))) || ((si1 > 0) && (si2 <= 0) && (si2 < (INT32_MIN / si1))) || ((si1 <= 0) && (si2 > 0) && (si1 < (INT32_MIN / si2))) || ((si1 <= 0) && (si2 <= 0) && (si1 != 0) && (si2 < (INT32_MAX / si1)))) ? + (UNDEFINED(si1)) : +#endif +#endif + si1 * si2; +} + +STATIC int32_t +FUNC_NAME(mod_func_int32_t_s_s)(int32_t si1, int32_t si2 LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + ((si2 == 0) || ((si1 == INT32_MIN) && (si2 == (-1)))) ? + (UNDEFINED(si1)) : +#endif + (si1 % si2); +} + +STATIC int32_t +FUNC_NAME(div_func_int32_t_s_s)(int32_t si1, int32_t si2 LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + ((si2 == 0) || ((si1 == INT32_MIN) && (si2 == (-1)))) ? + (UNDEFINED(si1)) : +#endif + (si1 / si2); +} + +STATIC int32_t +FUNC_NAME(lshift_func_int32_t_s_s)(int32_t left, int right LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + ((left < 0) || (((int)right) < 0) || (((int)right) >= 32) || (left > (INT32_MAX >> ((int)right)))) ? + (UNDEFINED(left)) : +#endif + (left << ((int)right)); +} + +STATIC int32_t +FUNC_NAME(lshift_func_int32_t_s_u)(int32_t left, unsigned int right LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + ((left < 0) || (((unsigned int)right) >= 32) || (left > (INT32_MAX >> ((unsigned int)right)))) ? + (UNDEFINED(left)) : +#endif + (left << ((unsigned int)right)); +} + +STATIC int32_t +FUNC_NAME(rshift_func_int32_t_s_s)(int32_t left, int right LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + ((left < 0) || (((int)right) < 0) || (((int)right) >= 32))? + (UNDEFINED(left)) : +#endif + (left >> ((int)right)); +} + +STATIC int32_t +FUNC_NAME(rshift_func_int32_t_s_u)(int32_t left, unsigned int right LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + ((left < 0) || (((unsigned int)right) >= 32)) ? + (UNDEFINED(left)) : +#endif + (left >> ((unsigned int)right)); +} + +#ifndef NO_LONGLONG + + +STATIC int64_t +FUNC_NAME(unary_minus_func_int64_t_s)(int64_t si LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE +#if (INT64_MAX>=INT_MAX) + (si==INT64_MIN) ? + (UNDEFINED(si)) : +#endif +#endif + -si; +} + +STATIC int64_t +FUNC_NAME(add_func_int64_t_s_s)(int64_t si1, int64_t si2 LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE +#if (INT64_MAX>=INT_MAX) + (((si1>0) && (si2>0) && (si1 > (INT64_MAX-si2))) || ((si1<0) && (si2<0) && (si1 < (INT64_MIN-si2)))) ? + (UNDEFINED(si1)) : +#endif +#endif + (si1 + si2); +} + +STATIC int64_t +FUNC_NAME(sub_func_int64_t_s_s)(int64_t si1, int64_t si2 LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE +#if (INT64_MAX>=INT_MAX) + (((si1^si2) & (((si1 ^ ((si1^si2) & (~INT64_MAX)))-si2)^si2)) < 0) ? + (UNDEFINED(si1)) : +#endif +#endif + (si1 - si2); +} + +STATIC int64_t +FUNC_NAME(mul_func_int64_t_s_s)(int64_t si1, int64_t si2 LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE +#if (INT64_MAX>=INT_MAX) + (((si1 > 0) && (si2 > 0) && (si1 > (INT64_MAX / si2))) || ((si1 > 0) && (si2 <= 0) && (si2 < (INT64_MIN / si1))) || ((si1 <= 0) && (si2 > 0) && (si1 < (INT64_MIN / si2))) || ((si1 <= 0) && (si2 <= 0) && (si1 != 0) && (si2 < (INT64_MAX / si1)))) ? + (UNDEFINED(si1)) : +#endif +#endif + si1 * si2; +} + +STATIC int64_t +FUNC_NAME(mod_func_int64_t_s_s)(int64_t si1, int64_t si2 LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + ((si2 == 0) || ((si1 == INT64_MIN) && (si2 == (-1)))) ? + (UNDEFINED(si1)) : +#endif + (si1 % si2); +} + +STATIC int64_t +FUNC_NAME(div_func_int64_t_s_s)(int64_t si1, int64_t si2 LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + ((si2 == 0) || ((si1 == INT64_MIN) && (si2 == (-1)))) ? + (UNDEFINED(si1)) : +#endif + (si1 / si2); +} + +STATIC int64_t +FUNC_NAME(lshift_func_int64_t_s_s)(int64_t left, int right LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + ((left < 0) || (((int)right) < 0) || (((int)right) >= 32) || (left > (INT64_MAX >> ((int)right)))) ? + (UNDEFINED(left)) : +#endif + (left << ((int)right)); +} + +STATIC int64_t +FUNC_NAME(lshift_func_int64_t_s_u)(int64_t left, unsigned int right LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + ((left < 0) || (((unsigned int)right) >= 32) || (left > (INT64_MAX >> ((unsigned int)right)))) ? + (UNDEFINED(left)) : +#endif + (left << ((unsigned int)right)); +} + +STATIC int64_t +FUNC_NAME(rshift_func_int64_t_s_s)(int64_t left, int right LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + ((left < 0) || (((int)right) < 0) || (((int)right) >= 32))? + (UNDEFINED(left)) : +#endif + (left >> ((int)right)); +} + +STATIC int64_t +FUNC_NAME(rshift_func_int64_t_s_u)(int64_t left, unsigned int right LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + ((left < 0) || (((unsigned int)right) >= 32)) ? + (UNDEFINED(left)) : +#endif + (left >> ((unsigned int)right)); +} + +#endif + + + + + +STATIC uint8_t +FUNC_NAME(unary_minus_func_uint8_t_u)(uint8_t ui LOG_INDEX) +{ + LOG_EXEC + return -ui; +} + +STATIC uint8_t +FUNC_NAME(add_func_uint8_t_u_u)(uint8_t ui1, uint8_t ui2 LOG_INDEX) +{ + LOG_EXEC + return ui1 + ui2; +} + +STATIC uint8_t +FUNC_NAME(sub_func_uint8_t_u_u)(uint8_t ui1, uint8_t ui2 LOG_INDEX) +{ + LOG_EXEC + return ui1 - ui2; +} + +STATIC uint8_t +FUNC_NAME(mul_func_uint8_t_u_u)(uint8_t ui1, uint8_t ui2 LOG_INDEX) +{ + LOG_EXEC + return ((unsigned int)ui1) * ((unsigned int)ui2); +} + +STATIC uint8_t +FUNC_NAME(mod_func_uint8_t_u_u)(uint8_t ui1, uint8_t ui2 LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + (ui2 == 0) ? + (UNDEFINED(ui1)) : +#endif + (ui1 % ui2); +} + +STATIC uint8_t +FUNC_NAME(div_func_uint8_t_u_u)(uint8_t ui1, uint8_t ui2 LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + (ui2 == 0) ? + (UNDEFINED(ui1)) : +#endif + (ui1 / ui2); +} + +STATIC uint8_t +FUNC_NAME(lshift_func_uint8_t_u_s)(uint8_t left, int right LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + ((((int)right) < 0) || (((int)right) >= 32) || (left > (UINT8_MAX >> ((int)right)))) ? + (UNDEFINED(left)) : +#endif + (left << ((int)right)); +} + +STATIC uint8_t +FUNC_NAME(lshift_func_uint8_t_u_u)(uint8_t left, unsigned int right LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + ((((unsigned int)right) >= 32) || (left > (UINT8_MAX >> ((unsigned int)right)))) ? + (UNDEFINED(left)) : +#endif + (left << ((unsigned int)right)); +} + +STATIC uint8_t +FUNC_NAME(rshift_func_uint8_t_u_s)(uint8_t left, int right LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + ((((int)right) < 0) || (((int)right) >= 32)) ? + (UNDEFINED(left)) : +#endif + (left >> ((int)right)); +} + +STATIC uint8_t +FUNC_NAME(rshift_func_uint8_t_u_u)(uint8_t left, unsigned int right LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + (((unsigned int)right) >= 32) ? + (UNDEFINED(left)) : +#endif + (left >> ((unsigned int)right)); +} + + + +STATIC uint16_t +FUNC_NAME(unary_minus_func_uint16_t_u)(uint16_t ui LOG_INDEX) +{ + LOG_EXEC + return -ui; +} + +STATIC uint16_t +FUNC_NAME(add_func_uint16_t_u_u)(uint16_t ui1, uint16_t ui2 LOG_INDEX) +{ + LOG_EXEC + return ui1 + ui2; +} + +STATIC uint16_t +FUNC_NAME(sub_func_uint16_t_u_u)(uint16_t ui1, uint16_t ui2 LOG_INDEX) +{ + LOG_EXEC + return ui1 - ui2; +} + +STATIC uint16_t +FUNC_NAME(mul_func_uint16_t_u_u)(uint16_t ui1, uint16_t ui2 LOG_INDEX) +{ + LOG_EXEC + return ((unsigned int)ui1) * ((unsigned int)ui2); +} + +STATIC uint16_t +FUNC_NAME(mod_func_uint16_t_u_u)(uint16_t ui1, uint16_t ui2 LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + (ui2 == 0) ? + (UNDEFINED(ui1)) : +#endif + (ui1 % ui2); +} + +STATIC uint16_t +FUNC_NAME(div_func_uint16_t_u_u)(uint16_t ui1, uint16_t ui2 LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + (ui2 == 0) ? + (UNDEFINED(ui1)) : +#endif + (ui1 / ui2); +} + +STATIC uint16_t +FUNC_NAME(lshift_func_uint16_t_u_s)(uint16_t left, int right LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + ((((int)right) < 0) || (((int)right) >= 32) || (left > (UINT16_MAX >> ((int)right)))) ? + (UNDEFINED(left)) : +#endif + (left << ((int)right)); +} + +STATIC uint16_t +FUNC_NAME(lshift_func_uint16_t_u_u)(uint16_t left, unsigned int right LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + ((((unsigned int)right) >= 32) || (left > (UINT16_MAX >> ((unsigned int)right)))) ? + (UNDEFINED(left)) : +#endif + (left << ((unsigned int)right)); +} + +STATIC uint16_t +FUNC_NAME(rshift_func_uint16_t_u_s)(uint16_t left, int right LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + ((((int)right) < 0) || (((int)right) >= 32)) ? + (UNDEFINED(left)) : +#endif + (left >> ((int)right)); +} + +STATIC uint16_t +FUNC_NAME(rshift_func_uint16_t_u_u)(uint16_t left, unsigned int right LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + (((unsigned int)right) >= 32) ? + (UNDEFINED(left)) : +#endif + (left >> ((unsigned int)right)); +} + + + +STATIC uint32_t +FUNC_NAME(unary_minus_func_uint32_t_u)(uint32_t ui LOG_INDEX) +{ + LOG_EXEC + return -ui; +} + +STATIC uint32_t +FUNC_NAME(add_func_uint32_t_u_u)(uint32_t ui1, uint32_t ui2 LOG_INDEX) +{ + LOG_EXEC + return ui1 + ui2; +} + +STATIC uint32_t +FUNC_NAME(sub_func_uint32_t_u_u)(uint32_t ui1, uint32_t ui2 LOG_INDEX) +{ + LOG_EXEC + return ui1 - ui2; +} + +STATIC uint32_t +FUNC_NAME(mul_func_uint32_t_u_u)(uint32_t ui1, uint32_t ui2 LOG_INDEX) +{ + LOG_EXEC + return ((unsigned int)ui1) * ((unsigned int)ui2); +} + +STATIC uint32_t +FUNC_NAME(mod_func_uint32_t_u_u)(uint32_t ui1, uint32_t ui2 LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + (ui2 == 0) ? + (UNDEFINED(ui1)) : +#endif + (ui1 % ui2); +} + +STATIC uint32_t +FUNC_NAME(div_func_uint32_t_u_u)(uint32_t ui1, uint32_t ui2 LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + (ui2 == 0) ? + (UNDEFINED(ui1)) : +#endif + (ui1 / ui2); +} + +STATIC uint32_t +FUNC_NAME(lshift_func_uint32_t_u_s)(uint32_t left, int right LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + ((((int)right) < 0) || (((int)right) >= 32) || (left > (UINT32_MAX >> ((int)right)))) ? + (UNDEFINED(left)) : +#endif + (left << ((int)right)); +} + +STATIC uint32_t +FUNC_NAME(lshift_func_uint32_t_u_u)(uint32_t left, unsigned int right LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + ((((unsigned int)right) >= 32) || (left > (UINT32_MAX >> ((unsigned int)right)))) ? + (UNDEFINED(left)) : +#endif + (left << ((unsigned int)right)); +} + +STATIC uint32_t +FUNC_NAME(rshift_func_uint32_t_u_s)(uint32_t left, int right LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + ((((int)right) < 0) || (((int)right) >= 32)) ? + (UNDEFINED(left)) : +#endif + (left >> ((int)right)); +} + +STATIC uint32_t +FUNC_NAME(rshift_func_uint32_t_u_u)(uint32_t left, unsigned int right LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + (((unsigned int)right) >= 32) ? + (UNDEFINED(left)) : +#endif + (left >> ((unsigned int)right)); +} + +#ifndef NO_LONGLONG + + +STATIC uint64_t +FUNC_NAME(unary_minus_func_uint64_t_u)(uint64_t ui LOG_INDEX) +{ + LOG_EXEC + return -ui; +} + +STATIC uint64_t +FUNC_NAME(add_func_uint64_t_u_u)(uint64_t ui1, uint64_t ui2 LOG_INDEX) +{ + LOG_EXEC + return ui1 + ui2; +} + +STATIC uint64_t +FUNC_NAME(sub_func_uint64_t_u_u)(uint64_t ui1, uint64_t ui2 LOG_INDEX) +{ + LOG_EXEC + return ui1 - ui2; +} + +STATIC uint64_t +FUNC_NAME(mul_func_uint64_t_u_u)(uint64_t ui1, uint64_t ui2 LOG_INDEX) +{ + LOG_EXEC + return ((unsigned long long int)ui1) * ((unsigned long long int)ui2); +} + +STATIC uint64_t +FUNC_NAME(mod_func_uint64_t_u_u)(uint64_t ui1, uint64_t ui2 LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + (ui2 == 0) ? + (UNDEFINED(ui1)) : +#endif + (ui1 % ui2); +} + +STATIC uint64_t +FUNC_NAME(div_func_uint64_t_u_u)(uint64_t ui1, uint64_t ui2 LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + (ui2 == 0) ? + (UNDEFINED(ui1)) : +#endif + (ui1 / ui2); +} + +STATIC uint64_t +FUNC_NAME(lshift_func_uint64_t_u_s)(uint64_t left, int right LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + ((((int)right) < 0) || (((int)right) >= 32) || (left > (UINT64_MAX >> ((int)right)))) ? + (UNDEFINED(left)) : +#endif + (left << ((int)right)); +} + +STATIC uint64_t +FUNC_NAME(lshift_func_uint64_t_u_u)(uint64_t left, unsigned int right LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + ((((unsigned int)right) >= 32) || (left > (UINT64_MAX >> ((unsigned int)right)))) ? + (UNDEFINED(left)) : +#endif + (left << ((unsigned int)right)); +} + +STATIC uint64_t +FUNC_NAME(rshift_func_uint64_t_u_s)(uint64_t left, int right LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + ((((int)right) < 0) || (((int)right) >= 32)) ? + (UNDEFINED(left)) : +#endif + (left >> ((int)right)); +} + +STATIC uint64_t +FUNC_NAME(rshift_func_uint64_t_u_u)(uint64_t left, unsigned int right LOG_INDEX) +{ + LOG_EXEC + return +#ifndef UNSAFE + (((unsigned int)right) >= 32) ? + (UNDEFINED(left)) : +#endif + (left >> ((unsigned int)right)); +} + +#endif + +#endif diff --git a/tests/gears.png b/tests/gears.png Binary files differindex ffb855c4..a35db625 100644 --- a/tests/gears.png +++ b/tests/gears.png diff --git a/tests/gl_ps.c b/tests/gl_ps.c index 81579c1d..6ea0e3db 100644 --- a/tests/gl_ps.c +++ b/tests/gl_ps.c @@ -78,6 +78,9 @@ void shaders() { glLinkProgram(program); glGetProgramiv(program, GL_LINK_STATUS, &ok); assert(ok); + assert(glIsProgram(program)); + assert(!glIsProgram(0)); + assert(!glIsProgram(program+1)); // a number that can't be a real shader glUseProgram(program); diff --git a/tests/gl_ps.png b/tests/gl_ps.png Binary files differindex 185f7166..ac13ec0b 100644 --- a/tests/gl_ps.png +++ b/tests/gl_ps.png diff --git a/tests/gl_ps_packed.c b/tests/gl_ps_packed.c new file mode 100644 index 00000000..9ab99cb8 --- /dev/null +++ b/tests/gl_ps_packed.c @@ -0,0 +1,230 @@ +/******************************************************************* + * * + * Using SDL With OpenGL * + * * + * Tutorial by Kyle Foley (sdw) * + * * + * http://gpwiki.org/index.php/SDL:Tutorials:Using_SDL_with_OpenGL * + * * + *******************************************************************/ + +/* +THIS WORK, INCLUDING THE SOURCE CODE, DOCUMENTATION +AND RELATED MEDIA AND DATA, IS PLACED INTO THE PUBLIC DOMAIN. + +THE ORIGINAL AUTHOR IS KYLE FOLEY. + +THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY +OF ANY KIND, NOT EVEN THE IMPLIED WARRANTY OF +MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE, +ASSUMES _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE +RESULTING FROM THE USE, MODIFICATION, OR +REDISTRIBUTION OF THIS SOFTWARE. +*/ + +#if !EMSCRIPTEN +#define USE_GLEW 1 +#endif + +#if USE_GLEW +#include "GL/glew.h" +#endif + +#include "SDL/SDL.h" +#include "SDL/SDL_image.h" +#if !USE_GLEW +#include "SDL/SDL_opengl.h" +#endif + +#include <stdio.h> +#include <string.h> +#include <assert.h> + +void shaders() { +#if USE_GLEW + glewInit(); +#endif + + GLint ok; + + const char *vertexShader = "void main(void) \n" + "{ \n" + " gl_Position = ftransform(); \n" + " gl_TexCoord[0] = gl_MultiTexCoord0; \n" + " gl_FrontColor = gl_Color; \n" + "} \n"; + const char *fragmentShader = "uniform sampler2D tex0; \n" + "void main(void) \n" + "{ \n" + " gl_FragColor = gl_Color * texture2D(tex0, gl_TexCoord[0].xy); \n" + "} \n"; + + GLuint vs = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vs, 1, &vertexShader, NULL); + glCompileShader(vs); + glGetShaderiv(vs, GL_COMPILE_STATUS, &ok); + assert(ok); + + GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fs, 1, &fragmentShader, NULL); + glCompileShader(fs); + glGetShaderiv(fs, GL_COMPILE_STATUS, &ok); + assert(ok); + + GLuint program = glCreateProgram(); + + glAttachShader(program, vs); + glAttachShader(program, fs); + glLinkProgram(program); + glGetProgramiv(program, GL_LINK_STATUS, &ok); + assert(ok); + + glUseProgram(program); +} + +int main(int argc, char *argv[]) +{ + SDL_Surface *screen; + + // Slightly different SDL initialization + if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) { + printf("Unable to initialize SDL: %s\n", SDL_GetError()); + return 1; + } + + SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // *new* + + screen = SDL_SetVideoMode( 640, 480, 16, SDL_OPENGL ); // *changed* + if ( !screen ) { + printf("Unable to set video mode: %s\n", SDL_GetError()); + return 1; + } + + // Set the OpenGL state after creating the context with SDL_SetVideoMode + + glClearColor( 0, 0, 0, 0 ); + +#if !EMSCRIPTEN + glEnable( GL_TEXTURE_2D ); // Need this to display a texture XXX unnecessary in OpenGL ES 2.0/WebGL +#endif + + glViewport( 0, 0, 640, 480 ); + + glMatrixMode( GL_PROJECTION ); + GLfloat matrixData[] = { 2.0/640, 0, 0, 0, + 0, -2.0/480, 0, 0, + 0, 0, -1, 0, + -1, 1, 0, 1 }; + glLoadMatrixf(matrixData); // test loadmatrix + + glMatrixMode( GL_MODELVIEW ); + glLoadIdentity(); + + // Load the OpenGL texture + + GLuint texture; // Texture object handle + SDL_Surface *surface; // Gives us the information to make the texture + + if ( (surface = IMG_Load("screenshot.png")) ) { + + // Check that the image's width is a power of 2 + if ( (surface->w & (surface->w - 1)) != 0 ) { + printf("warning: image.bmp's width is not a power of 2\n"); + } + + // Also check if the height is a power of 2 + if ( (surface->h & (surface->h - 1)) != 0 ) { + printf("warning: image.bmp's height is not a power of 2\n"); + } + + // Have OpenGL generate a texture object handle for us + glGenTextures( 1, &texture ); + + // Bind the texture object + glBindTexture( GL_TEXTURE_2D, texture ); + + // Set the texture's stretching properties + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); + + //SDL_LockSurface(surface); + + // Add some greyness + memset(surface->pixels, 0x66, surface->w*surface->h); + + // Edit the texture object's image data using the information SDL_Surface gives us + glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0, + GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels ); + + //SDL_UnlockSurface(surface); + } + else { + printf("SDL could not load image.bmp: %s\n", SDL_GetError()); + SDL_Quit(); + return 1; + } + + // Free the SDL_Surface only if it was successfully created + if ( surface ) { + SDL_FreeSurface( surface ); + } + + // Clear the screen before drawing + glClear( GL_COLOR_BUFFER_BIT ); + + shaders(); + + // Bind the texture to which subsequent calls refer to + glBindTexture( GL_TEXTURE_2D, texture ); + + // Use clientside vertex pointers to render two items. In this test we have each + // attribute in a separate buffer, packed (i.e. stride == 0) + GLfloat vertexData[] = { 10, 10, + 300, 10, + 300, 128, + 10, 128, + 410, 10, + 600, 10, + 630, 200, + 310, 250, + 100, 300, + 300, 300, + 300, 400, + 100, 400 }; + GLfloat textureData[] = { 0, 0, + 1, 0, + 1, 1, + 0, 1, + 0, 0.5, + 1, 0.5, + 1, 1, + 0.5, 1, + 0, 0, + 1, 0, + 1, 1, + 0, 1, }; + + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glTexCoordPointer(2, GL_FLOAT, 0, textureData); + glEnableClientState(GL_VERTEX_ARRAY); + glVertexPointer(2, GL_FLOAT, 0, vertexData); + + glDrawArrays(GL_QUADS, 0, 12); + + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + glDisableClientState(GL_VERTEX_ARRAY); + + SDL_GL_SwapBuffers(); + +#if !EMSCRIPTEN + // Wait for 3 seconds to give us a chance to see the image + SDL_Delay(3000); +#endif + + // Now we can delete the OpenGL texture and close down SDL + glDeleteTextures( 1, &texture ); + + SDL_Quit(); + + return 0; +} diff --git a/tests/gl_ps_workaround.c b/tests/gl_ps_workaround.c new file mode 100644 index 00000000..1e2a5c41 --- /dev/null +++ b/tests/gl_ps_workaround.c @@ -0,0 +1,230 @@ +/******************************************************************* + * * + * Using SDL With OpenGL * + * * + * Tutorial by Kyle Foley (sdw) * + * * + * http://gpwiki.org/index.php/SDL:Tutorials:Using_SDL_with_OpenGL * + * * + *******************************************************************/ + +/* +THIS WORK, INCLUDING THE SOURCE CODE, DOCUMENTATION +AND RELATED MEDIA AND DATA, IS PLACED INTO THE PUBLIC DOMAIN. + +THE ORIGINAL AUTHOR IS KYLE FOLEY. + +THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY +OF ANY KIND, NOT EVEN THE IMPLIED WARRANTY OF +MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE, +ASSUMES _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE +RESULTING FROM THE USE, MODIFICATION, OR +REDISTRIBUTION OF THIS SOFTWARE. +*/ + +#if !EMSCRIPTEN +#define USE_GLEW 1 +#endif + +#if USE_GLEW +#include "GL/glew.h" +#endif + +#include "SDL/SDL.h" +#include "SDL/SDL_image.h" +#if !USE_GLEW +#include "SDL/SDL_opengl.h" +#endif + +#include <stdio.h> +#include <string.h> +#include <assert.h> + +void shaders() { +#if USE_GLEW + glewInit(); +#endif + + GLint ok; + + const char *vertexShader = "void main(void) \n" + "{ \n" + " gl_Position = ftransform(); \n" + " gl_TexCoord[0] = gl_MultiTexCoord0; \n" + " gl_FrontColor = gl_Color; \n" + "} \n"; + const char *fragmentShader = "uniform sampler2D tex0; \n" + "void main(void) \n" + "{ \n" + " gl_FragColor = gl_Color * texture2D(tex0, gl_TexCoord[0].xy); \n" + "} \n"; + + GLuint vs = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vs, 1, &vertexShader, NULL); + glCompileShader(vs); + glGetShaderiv(vs, GL_COMPILE_STATUS, &ok); + assert(ok); + + GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fs, 1, &fragmentShader, NULL); + glCompileShader(fs); + glGetShaderiv(fs, GL_COMPILE_STATUS, &ok); + assert(ok); + + GLuint program = glCreateProgram(); + + glAttachShader(program, vs); + glAttachShader(program, fs); + glLinkProgram(program); + glGetProgramiv(program, GL_LINK_STATUS, &ok); + assert(ok); + + glUseProgram(program); +} + +int main(int argc, char *argv[]) +{ + SDL_Surface *screen; + + // Slightly different SDL initialization + if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) { + printf("Unable to initialize SDL: %s\n", SDL_GetError()); + return 1; + } + + SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // *new* + + screen = SDL_SetVideoMode( 640, 480, 16, SDL_OPENGL ); // *changed* + if ( !screen ) { + printf("Unable to set video mode: %s\n", SDL_GetError()); + return 1; + } + + // Set the OpenGL state after creating the context with SDL_SetVideoMode + + glClearColor( 0, 0, 0, 0 ); + +#if !EMSCRIPTEN + glEnable( GL_TEXTURE_2D ); // Need this to display a texture XXX unnecessary in OpenGL ES 2.0/WebGL +#endif + + glViewport( 0, 0, 640, 480 ); + + glMatrixMode( GL_PROJECTION ); + GLfloat matrixData[] = { 2.0/640, 0, 0, 0, + 0, -2.0/480, 0, 0, + 0, 0, -1, 0, + -1, 1, 0, 1 }; + glLoadMatrixf(matrixData); // test loadmatrix + + glMatrixMode( GL_MODELVIEW ); + glLoadIdentity(); + + // Load the OpenGL texture + + GLuint texture; // Texture object handle + SDL_Surface *surface; // Gives us the information to make the texture + + if ( (surface = IMG_Load("screenshot.png")) ) { + + // Check that the image's width is a power of 2 + if ( (surface->w & (surface->w - 1)) != 0 ) { + printf("warning: image.bmp's width is not a power of 2\n"); + } + + // Also check if the height is a power of 2 + if ( (surface->h & (surface->h - 1)) != 0 ) { + printf("warning: image.bmp's height is not a power of 2\n"); + } + + // Have OpenGL generate a texture object handle for us + glGenTextures( 1, &texture ); + + // Bind the texture object + glBindTexture( GL_TEXTURE_2D, texture ); + + // Set the texture's stretching properties + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); + + //SDL_LockSurface(surface); + + // Add some greyness + memset(surface->pixels, 0x66, surface->w*surface->h); + + // Edit the texture object's image data using the information SDL_Surface gives us + glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0, + GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels ); + + //SDL_UnlockSurface(surface); + } + else { + printf("SDL could not load image.bmp: %s\n", SDL_GetError()); + SDL_Quit(); + return 1; + } + + // Free the SDL_Surface only if it was successfully created + if ( surface ) { + SDL_FreeSurface( surface ); + } + + // Clear the screen before drawing + glClear( GL_COLOR_BUFFER_BIT ); + + shaders(); + + // Bind the texture to which subsequent calls refer to + glBindTexture( GL_TEXTURE_2D, texture ); + + // Use clientside vertex pointers to render two items. In this test we have each + // attribute in a separate buffer, packed (i.e. stride == 0) + GLfloat vertexData[] = { 10, 10, + 300, 10, + 300, 128, + 10, 128, + 410, 10, + 600, 10, + 630, 200, + 310, 250, + 100, 300, + 300, 300, + 300, 400, + 100, 400 }; + GLfloat textureData[] = { 0, 0, + 1, 0, + 1, 1, + 0, 1, + 0, 0.5, + 1, 0.5, + 1, 1, + 0.5, 1, + 0, 0, + 1, 0, + 1, 1, + 0, 1, }; + + glEnableClientState(GL_TEXTURE_2D); // XXX should be GL_TEXTURE_COORD_ARRAY); // XXX + glTexCoordPointer(2, GL_FLOAT, 0, textureData); + glEnableClientState(GL_VERTEX_ARRAY); + glVertexPointer(2, GL_FLOAT, 0, vertexData); + + glDrawArrays(GL_QUADS, 0, 12); + + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + glDisableClientState(GL_VERTEX_ARRAY); + + SDL_GL_SwapBuffers(); + +#if !EMSCRIPTEN + // Wait for 3 seconds to give us a chance to see the image + SDL_Delay(3000); +#endif + + // Now we can delete the OpenGL texture and close down SDL + glDeleteTextures( 1, &texture ); + + SDL_Quit(); + + return 0; +} diff --git a/tests/gl_ps_workaround2.c b/tests/gl_ps_workaround2.c new file mode 100644 index 00000000..e5bd2fd1 --- /dev/null +++ b/tests/gl_ps_workaround2.c @@ -0,0 +1,230 @@ +/******************************************************************* + * * + * Using SDL With OpenGL * + * * + * Tutorial by Kyle Foley (sdw) * + * * + * http://gpwiki.org/index.php/SDL:Tutorials:Using_SDL_with_OpenGL * + * * + *******************************************************************/ + +/* +THIS WORK, INCLUDING THE SOURCE CODE, DOCUMENTATION +AND RELATED MEDIA AND DATA, IS PLACED INTO THE PUBLIC DOMAIN. + +THE ORIGINAL AUTHOR IS KYLE FOLEY. + +THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY +OF ANY KIND, NOT EVEN THE IMPLIED WARRANTY OF +MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE, +ASSUMES _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE +RESULTING FROM THE USE, MODIFICATION, OR +REDISTRIBUTION OF THIS SOFTWARE. +*/ + +#if !EMSCRIPTEN +#define USE_GLEW 1 +#endif + +#if USE_GLEW +#include "GL/glew.h" +#endif + +#include "SDL/SDL.h" +#include "SDL/SDL_image.h" +#if !USE_GLEW +#include "SDL/SDL_opengl.h" +#endif + +#include <stdio.h> +#include <string.h> +#include <assert.h> + +void shaders() { +#if USE_GLEW + glewInit(); +#endif + + GLint ok; + + const char *vertexShader = "void main(void) \n" + "{ \n" + " gl_Position = ftransform(); \n" + " gl_TexCoord[0] = gl_MultiTexCoord0; \n" + " gl_FrontColor = gl_Color; \n" + "} \n"; + const char *fragmentShader = "uniform sampler2D tex0; \n" + "void main(void) \n" + "{ \n" + " gl_FragColor = gl_Color * texture2D(tex0, gl_TexCoord[0].xy); \n" + "} \n"; + + GLuint vs = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vs, 1, &vertexShader, NULL); + glCompileShader(vs); + glGetShaderiv(vs, GL_COMPILE_STATUS, &ok); + assert(ok); + + GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fs, 1, &fragmentShader, NULL); + glCompileShader(fs); + glGetShaderiv(fs, GL_COMPILE_STATUS, &ok); + assert(ok); + + GLuint program = glCreateProgram(); + + glAttachShader(program, vs); + glAttachShader(program, fs); + glLinkProgram(program); + glGetProgramiv(program, GL_LINK_STATUS, &ok); + assert(ok); + + glUseProgram(program); +} + +int main(int argc, char *argv[]) +{ + SDL_Surface *screen; + + // Slightly different SDL initialization + if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) { + printf("Unable to initialize SDL: %s\n", SDL_GetError()); + return 1; + } + + SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // *new* + + screen = SDL_SetVideoMode( 640, 480, 16, SDL_OPENGL ); // *changed* + if ( !screen ) { + printf("Unable to set video mode: %s\n", SDL_GetError()); + return 1; + } + + // Set the OpenGL state after creating the context with SDL_SetVideoMode + + glClearColor( 0, 0, 0, 0 ); + +#if !EMSCRIPTEN + glEnable( GL_TEXTURE_2D ); // Need this to display a texture XXX unnecessary in OpenGL ES 2.0/WebGL +#endif + + glViewport( 0, 0, 640, 480 ); + + glMatrixMode( GL_PROJECTION ); + GLfloat matrixData[] = { 2.0/640, 0, 0, 0, + 0, -2.0/480, 0, 0, + 0, 0, -1, 0, + -1, 1, 0, 1 }; + glLoadMatrixf(matrixData); // test loadmatrix + + glMatrixMode( GL_MODELVIEW ); + glLoadIdentity(); + + // Load the OpenGL texture + + GLuint texture; // Texture object handle + SDL_Surface *surface; // Gives us the information to make the texture + + if ( (surface = IMG_Load("screenshot.png")) ) { + + // Check that the image's width is a power of 2 + if ( (surface->w & (surface->w - 1)) != 0 ) { + printf("warning: image.bmp's width is not a power of 2\n"); + } + + // Also check if the height is a power of 2 + if ( (surface->h & (surface->h - 1)) != 0 ) { + printf("warning: image.bmp's height is not a power of 2\n"); + } + + // Have OpenGL generate a texture object handle for us + glGenTextures( 1, &texture ); + + // Bind the texture object + glBindTexture( GL_TEXTURE_2D, texture ); + + // Set the texture's stretching properties + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); + + //SDL_LockSurface(surface); + + // Add some greyness + memset(surface->pixels, 0x66, surface->w*surface->h); + + // Edit the texture object's image data using the information SDL_Surface gives us + glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0, + GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels ); + + //SDL_UnlockSurface(surface); + } + else { + printf("SDL could not load image.bmp: %s\n", SDL_GetError()); + SDL_Quit(); + return 1; + } + + // Free the SDL_Surface only if it was successfully created + if ( surface ) { + SDL_FreeSurface( surface ); + } + + // Clear the screen before drawing + glClear( GL_COLOR_BUFFER_BIT ); + + shaders(); + + // Bind the texture to which subsequent calls refer to + glBindTexture( GL_TEXTURE_2D, texture ); + + // Use clientside vertex pointers to render two items. In this test we have each + // attribute in a separate buffer, packed (i.e. stride == 0) + GLfloat vertexData[] = { 10, 10, + 300, 10, + 300, 128, + 10, 128, + 410, 10, + 600, 10, + 630, 200, + 310, 250, + 100, 300, + 300, 300, + 300, 400, + 100, 400 }; + GLfloat textureData[] = { 0, 0, + 1, 0, + 1, 1, + 0, 1, + 0, 0.5, + 1, 0.5, + 1, 1, + 0.5, 1, + 0, 0, + 1, 0, + 1, 1, + 0, 1, }; + + glEnable(GL_TEXTURE_2D); // XXX should be GL_TEXTURE_COORD_ARRAY); and also glEnableClientState! XXX two workarounds here + glTexCoordPointer(2, GL_FLOAT, 0, textureData); + glEnableClientState(GL_VERTEX_ARRAY); + glVertexPointer(2, GL_FLOAT, 0, vertexData); + + glDrawArrays(GL_QUADS, 0, 12); + + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + glDisableClientState(GL_VERTEX_ARRAY); + + SDL_GL_SwapBuffers(); + +#if !EMSCRIPTEN + // Wait for 3 seconds to give us a chance to see the image + SDL_Delay(3000); +#endif + + // Now we can delete the OpenGL texture and close down SDL + glDeleteTextures( 1, &texture ); + + SDL_Quit(); + + return 0; +} diff --git a/tests/glbook/CH13_ParticleSystem.png b/tests/glbook/CH13_ParticleSystem.png Binary files differindex ff9c3496..4b69414f 100644 --- a/tests/glbook/CH13_ParticleSystem.png +++ b/tests/glbook/CH13_ParticleSystem.png diff --git a/tests/glbook/Chapter_13/ParticleSystem/ParticleSystem_orig.c b/tests/glbook/Chapter_13/ParticleSystem/ParticleSystem_orig.c index 378d05a7..02fddd9c 100644 --- a/tests/glbook/Chapter_13/ParticleSystem/ParticleSystem_orig.c +++ b/tests/glbook/Chapter_13/ParticleSystem/ParticleSystem_orig.c @@ -17,9 +17,16 @@ #include <math.h>
#include "esUtil.h"
-#define NUM_PARTICLES 1000
+#define NUM_PARTICLES 2000
#define PARTICLE_SIZE 7
+int randomTemp = 8765;
+float myrandom() {
+ int curr = randomTemp;
+ randomTemp = (1140671485 * randomTemp + 12820163) % 4294967296;
+ return ((float)curr) / 4294967296;
+}
+
typedef struct
{
// Handle to a program object
@@ -145,17 +152,17 @@ int Init ( ESContext *esContext ) float *particleData = &userData->particleData[i * PARTICLE_SIZE];
// Lifetime of particle
- (*particleData++) = ( (float)(rand() % 10000) / 10000.0f );
+ (*particleData++) = myrandom();
// End position of particle
- (*particleData++) = ( (float)(rand() % 10000) / 5000.0f ) - 1.0f;
- (*particleData++) = ( (float)(rand() % 10000) / 5000.0f ) - 1.0f;
- (*particleData++) = ( (float)(rand() % 10000) / 5000.0f ) - 1.0f;
+ (*particleData++) = myrandom() * 2 - 1.0f;
+ (*particleData++) = myrandom() * 2 - 1.0f;
+ (*particleData++) = myrandom() * 2 - 1.0f;
// Start position of particle
- (*particleData++) = ( (float)(rand() % 10000) / 40000.0f ) - 0.125f;
- (*particleData++) = ( (float)(rand() % 10000) / 40000.0f ) - 0.125f;
- (*particleData++) = ( (float)(rand() % 10000) / 40000.0f ) - 0.125f;
+ (*particleData++) = myrandom() * 0.25 - 0.125f;
+ (*particleData++) = myrandom() * 0.25 - 0.125f;
+ (*particleData++) = myrandom() * 0.25 - 0.125f;
}
@@ -188,17 +195,17 @@ void Update ( ESContext *esContext, float deltaTime ) userData->time = 0.0f;
// Pick a new start location and color
- centerPos[0] = ( (float)(rand() % 10000) / 10000.0f ) - 0.5f;
- centerPos[1] = ( (float)(rand() % 10000) / 10000.0f ) - 0.5f;
- centerPos[2] = ( (float)(rand() % 10000) / 10000.0f ) - 0.5f;
+ centerPos[0] = myrandom() - 0.5f;
+ centerPos[1] = myrandom() - 0.5f;
+ centerPos[2] = myrandom() - 0.5f;
glUniform3fv ( userData->centerPositionLoc, 1, ¢erPos[0] );
// Random color
- color[0] = ( (float)(rand() % 10000) / 20000.0f ) + 0.5f;
- color[1] = ( (float)(rand() % 10000) / 20000.0f ) + 0.5f;
- color[2] = ( (float)(rand() % 10000) / 20000.0f ) + 0.5f;
- color[3] = 0.5;
+ color[0] = myrandom() * 0.5 + 0.5f;
+ color[1] = myrandom() * 0.5 + 0.5f;
+ color[2] = myrandom() * 0.5 + 0.5f;
+ color[3] = 1.0;
glUniform4fv ( userData->colorLoc, 1, &color[0] );
}
@@ -252,6 +259,8 @@ void Draw ( ESContext *esContext ) // Set the sampler texture unit to 0
glUniform1i ( userData->samplerLoc, 0 );
+ Update ( esContext, 133 * 0.001125 );
+
glDrawArrays( GL_POINTS, 0, NUM_PARTICLES );
eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface );
@@ -280,15 +289,18 @@ int main ( int argc, char *argv[] ) esInitContext ( &esContext );
esContext.userData = &userData;
- esCreateWindow ( &esContext, "ParticleSystem", 640, 480, ES_WINDOW_RGB );
+ esCreateWindow ( &esContext, "ParticleSystem", 320, 240, ES_WINDOW_RGB );
if ( !Init ( &esContext ) )
return 0;
esRegisterDrawFunc ( &esContext, Draw );
- esRegisterUpdateFunc ( &esContext, Update );
+ //esRegisterUpdateFunc ( &esContext, Update );
- esMainLoop ( &esContext );
+ Draw (&esContext);
+ Draw (&esContext);
+
+ //esMainLoop ( &esContext );
ShutDown ( &esContext );
}
diff --git a/tests/glbook/Chapter_2/Hello_Triangle/Hello_Triangle_orig.c b/tests/glbook/Chapter_2/Hello_Triangle/Hello_Triangle_orig.c index fd4c506a..cc25a559 100644 --- a/tests/glbook/Chapter_2/Hello_Triangle/Hello_Triangle_orig.c +++ b/tests/glbook/Chapter_2/Hello_Triangle/Hello_Triangle_orig.c @@ -171,7 +171,7 @@ void Draw ( ESContext *esContext ) glVertexAttribPointer ( 0, 3, GL_FLOAT, GL_FALSE, 0, vVertices ); glEnableVertexAttribArray ( 0 ); - glDrawArrays ( GL_TRIANGLES, 0, 3 ); + glDrawArrays ( GL_TRIANGLES, 0, 3 ); // TODO: need glDrawElements! } int main ( int argc, char *argv[] ) diff --git a/tests/glbook/Chapter_9/TextureWrap/TextureWrap_orig.c b/tests/glbook/Chapter_9/TextureWrap/TextureWrap_orig.c index fe22282f..e3531f04 100644 --- a/tests/glbook/Chapter_9/TextureWrap/TextureWrap_orig.c +++ b/tests/glbook/Chapter_9/TextureWrap/TextureWrap_orig.c @@ -243,7 +243,7 @@ int main ( int argc, char *argv[] ) esInitContext ( &esContext ); esContext.userData = &userData; - esCreateWindow ( &esContext, "MipMap 2D", 640, 480, ES_WINDOW_RGB ); + esCreateWindow ( &esContext, "MipMap 2D", 320, 240, ES_WINDOW_RGB ); if ( !Init ( &esContext ) ) return 0; diff --git a/tests/hello_libcxx_mod2.cpp b/tests/hello_libcxx_mod2.cpp new file mode 100644 index 00000000..b18a523a --- /dev/null +++ b/tests/hello_libcxx_mod2.cpp @@ -0,0 +1,10 @@ +#include <iostream> +#include <emscripten.h> + +int main() +{ + std::cout << "hello, world!" << std::endl; + emscripten_jcache_printf("waka %d waka\n", 5); + return 0; +} + diff --git a/tests/hello_libcxx_mod2a.cpp b/tests/hello_libcxx_mod2a.cpp new file mode 100644 index 00000000..f48ad4fe --- /dev/null +++ b/tests/hello_libcxx_mod2a.cpp @@ -0,0 +1,11 @@ +#include <iostream> +#include <emscripten.h> + +int main() +{ + std::cout << "hello, world!" << std::endl; + emscripten_jcache_printf("waka %d waka\n", 5); + emscripten_jcache_printf("yet another printf %.2f %d\n", 5.5, 66); + return 0; +} + diff --git a/tests/hello_world_gles_full.c b/tests/hello_world_gles_full.c new file mode 100644 index 00000000..cca66506 --- /dev/null +++ b/tests/hello_world_gles_full.c @@ -0,0 +1,742 @@ +/* + * Copyright (C) 1999-2001 Brian Paul All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +/* + * Ported to GLES2. + * Kristian Høgsberg <krh@bitplanet.net> + * May 3, 2010 + * + * Improve GLES2 port: + * * Refactor gear drawing. + * * Use correct normals for surfaces. + * * Improve shader. + * * Use perspective projection transformation. + * * Add FPS count. + * * Add comments. + * Alexandros Frantzis <alexandros.frantzis@linaro.org> + * Jul 13, 2010 + */ + +#define GL_GLEXT_PROTOTYPES +#define EGL_EGLEXT_PROTOTYPES + +#define _GNU_SOURCE + +#include <math.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <sys/time.h> +#include <unistd.h> +#ifdef __APPLE__ +#include <OpenGL/gl.h> +#include <Glut/glut.h> +#else +#include <GL/gl.h> +#include <GL/glut.h> +#endif + +#define STRIPS_PER_TOOTH 7 +#define VERTICES_PER_TOOTH 34 +#define GEAR_VERTEX_STRIDE 6 + +#ifndef HAVE_BUILTIN_SINCOS +#define sincos _sincos +static void +sincos (double a, double *s, double *c) +{ + *s = sin (a); + *c = cos (a); +} +#endif + +/** + * Struct describing the vertices in triangle strip + */ +struct vertex_strip { + /** The first vertex in the strip */ + GLint first; + /** The number of consecutive vertices in the strip after the first */ + GLint count; +}; + +/* Each vertex consist of GEAR_VERTEX_STRIDE GLfloat attributes */ +typedef GLfloat GearVertex[GEAR_VERTEX_STRIDE]; + +/** + * Struct representing a gear. + */ +struct gear { + /** The array of vertices comprising the gear */ + GearVertex *vertices; + /** The number of vertices comprising the gear */ + int nvertices; + /** The array of triangle strips comprising the gear */ + struct vertex_strip *strips; + /** The number of triangle strips comprising the gear */ + int nstrips; + /** The Vertex Buffer Object holding the vertices in the graphics card */ + GLuint vbo; +}; + +/** The view rotation [x, y, z] */ +static GLfloat view_rot[3] = { 20.0, 30.0, 0.0 }; +/** The gears */ +static struct gear *gear1, *gear2, *gear3; +/** The current gear rotation angle */ +static GLfloat angle = 0.0; +/** The location of the shader uniforms */ +static GLuint ModelViewProjectionMatrix_location, + NormalMatrix_location, + LightSourcePosition_location, + MaterialColor_location; +/** The projection matrix */ +static GLfloat ProjectionMatrix[16]; +/** The direction of the directional light for the scene */ +static const GLfloat LightSourcePosition[4] = { 5.0, 5.0, 10.0, 1.0}; + +/** + * Fills a gear vertex. + * + * @param v the vertex to fill + * @param x the x coordinate + * @param y the y coordinate + * @param z the z coortinate + * @param n pointer to the normal table + * + * @return the operation error code + */ +static GearVertex * +vert(GearVertex *v, GLfloat x, GLfloat y, GLfloat z, GLfloat n[3]) +{ + v[0][0] = x; + v[0][1] = y; + v[0][2] = z; + v[0][3] = n[0]; + v[0][4] = n[1]; + v[0][5] = n[2]; + + return v + 1; +} + +/** + * Create a gear wheel. + * + * @param inner_radius radius of hole at center + * @param outer_radius radius at center of teeth + * @param width width of gear + * @param teeth number of teeth + * @param tooth_depth depth of tooth + * + * @return pointer to the constructed struct gear + */ +static struct gear * +create_gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width, + GLint teeth, GLfloat tooth_depth) +{ + GLfloat r0, r1, r2; + GLfloat da; + GearVertex *v; + struct gear *gear; + double s[5], c[5]; + GLfloat normal[3]; + int cur_strip = 0; + int i; + + /* Allocate memory for the gear */ + gear = malloc(sizeof *gear); + if (gear == NULL) + return NULL; + + /* Calculate the radii used in the gear */ + r0 = inner_radius; + r1 = outer_radius - tooth_depth / 2.0; + r2 = outer_radius + tooth_depth / 2.0; + + da = 2.0 * M_PI / teeth / 4.0; + + /* Allocate memory for the triangle strip information */ + gear->nstrips = STRIPS_PER_TOOTH * teeth; + gear->strips = calloc(gear->nstrips, sizeof (*gear->strips)); + + /* Allocate memory for the vertices */ + gear->vertices = calloc(VERTICES_PER_TOOTH * teeth, sizeof(*gear->vertices)); + v = gear->vertices; + + for (i = 0; i < teeth; i++) { + /* Calculate needed sin/cos for varius angles */ + sincos(i * 2.0 * M_PI / teeth, &s[0], &c[0]); + sincos(i * 2.0 * M_PI / teeth + da, &s[1], &c[1]); + sincos(i * 2.0 * M_PI / teeth + da * 2, &s[2], &c[2]); + sincos(i * 2.0 * M_PI / teeth + da * 3, &s[3], &c[3]); + sincos(i * 2.0 * M_PI / teeth + da * 4, &s[4], &c[4]); + + /* A set of macros for making the creation of the gears easier */ +#define GEAR_POINT(r, da) { (r) * c[(da)], (r) * s[(da)] } +#define SET_NORMAL(x, y, z) do { \ + normal[0] = (x); normal[1] = (y); normal[2] = (z); \ +} while(0) + +#define GEAR_VERT(v, point, sign) vert((v), p[(point)].x, p[(point)].y, (sign) * width * 0.5, normal) + +#define START_STRIP do { \ + gear->strips[cur_strip].first = v - gear->vertices; \ +} while(0); + +#define END_STRIP do { \ + int _tmp = (v - gear->vertices); \ + gear->strips[cur_strip].count = _tmp - gear->strips[cur_strip].first; \ + cur_strip++; \ +} while (0) + +#define QUAD_WITH_NORMAL(p1, p2) do { \ + SET_NORMAL((p[(p1)].y - p[(p2)].y), -(p[(p1)].x - p[(p2)].x), 0); \ + v = GEAR_VERT(v, (p1), -1); \ + v = GEAR_VERT(v, (p1), 1); \ + v = GEAR_VERT(v, (p2), -1); \ + v = GEAR_VERT(v, (p2), 1); \ +} while(0) + + struct point { + GLfloat x; + GLfloat y; + }; + + /* Create the 7 points (only x,y coords) used to draw a tooth */ + struct point p[7] = { + GEAR_POINT(r2, 1), // 0 + GEAR_POINT(r2, 2), // 1 + GEAR_POINT(r1, 0), // 2 + GEAR_POINT(r1, 3), // 3 + GEAR_POINT(r0, 0), // 4 + GEAR_POINT(r1, 4), // 5 + GEAR_POINT(r0, 4), // 6 + }; + + /* Front face */ + START_STRIP; + SET_NORMAL(0, 0, 1.0); + v = GEAR_VERT(v, 0, +1); + v = GEAR_VERT(v, 1, +1); + v = GEAR_VERT(v, 2, +1); + v = GEAR_VERT(v, 3, +1); + v = GEAR_VERT(v, 4, +1); + v = GEAR_VERT(v, 5, +1); + v = GEAR_VERT(v, 6, +1); + END_STRIP; + + /* Inner face */ + START_STRIP; + QUAD_WITH_NORMAL(4, 6); + END_STRIP; + + /* Back face */ + START_STRIP; + SET_NORMAL(0, 0, -1.0); + v = GEAR_VERT(v, 6, -1); + v = GEAR_VERT(v, 5, -1); + v = GEAR_VERT(v, 4, -1); + v = GEAR_VERT(v, 3, -1); + v = GEAR_VERT(v, 2, -1); + v = GEAR_VERT(v, 1, -1); + v = GEAR_VERT(v, 0, -1); + END_STRIP; + + /* Outer face */ + START_STRIP; + QUAD_WITH_NORMAL(0, 2); + END_STRIP; + + START_STRIP; + QUAD_WITH_NORMAL(1, 0); + END_STRIP; + + START_STRIP; + QUAD_WITH_NORMAL(3, 1); + END_STRIP; + + START_STRIP; + QUAD_WITH_NORMAL(5, 3); + END_STRIP; + } + + gear->nvertices = (v - gear->vertices); + + /* Store the vertices in a vertex buffer object (VBO) */ + glGenBuffers(1, &gear->vbo); + glBindBuffer(GL_ARRAY_BUFFER, gear->vbo); + glBufferData(GL_ARRAY_BUFFER, gear->nvertices * sizeof(GearVertex), + gear->vertices, GL_STATIC_DRAW); + + return gear; +} + +/** + * Multiplies two 4x4 matrices. + * + * The result is stored in matrix m. + * + * @param m the first matrix to multiply + * @param n the second matrix to multiply + */ +static void +multiply(GLfloat *m, const GLfloat *n) +{ + GLfloat tmp[16]; + const GLfloat *row, *column; + div_t d; + int i, j; + + for (i = 0; i < 16; i++) { + tmp[i] = 0; + d = div(i, 4); + row = n + d.quot * 4; + column = m + d.rem; + for (j = 0; j < 4; j++) + tmp[i] += row[j] * column[j * 4]; + } + memcpy(m, &tmp, sizeof tmp); +} + +/** + * Rotates a 4x4 matrix. + * + * @param[in,out] m the matrix to rotate + * @param angle the angle to rotate + * @param x the x component of the direction to rotate to + * @param y the y component of the direction to rotate to + * @param z the z component of the direction to rotate to + */ +static void +rotate(GLfloat *m, GLfloat angle, GLfloat x, GLfloat y, GLfloat z) +{ + double s, c; + + sincos(angle, &s, &c); + GLfloat r[16] = { + x * x * (1 - c) + c, y * x * (1 - c) + z * s, x * z * (1 - c) - y * s, 0, + x * y * (1 - c) - z * s, y * y * (1 - c) + c, y * z * (1 - c) + x * s, 0, + x * z * (1 - c) + y * s, y * z * (1 - c) - x * s, z * z * (1 - c) + c, 0, + 0, 0, 0, 1 + }; + + multiply(m, r); +} + + +/** + * Translates a 4x4 matrix. + * + * @param[in,out] m the matrix to translate + * @param x the x component of the direction to translate to + * @param y the y component of the direction to translate to + * @param z the z component of the direction to translate to + */ +static void +translate(GLfloat *m, GLfloat x, GLfloat y, GLfloat z) +{ + GLfloat t[16] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }; + + multiply(m, t); +} + +/** + * Creates an identity 4x4 matrix. + * + * @param m the matrix make an identity matrix + */ +static void +identity(GLfloat *m) +{ + GLfloat t[16] = { + 1.0, 0.0, 0.0, 0.0, + 0.0, 1.0, 0.0, 0.0, + 0.0, 0.0, 1.0, 0.0, + 0.0, 0.0, 0.0, 1.0, + }; + + memcpy(m, t, sizeof(t)); +} + +/** + * Transposes a 4x4 matrix. + * + * @param m the matrix to transpose + */ +static void +transpose(GLfloat *m) +{ + GLfloat t[16] = { + m[0], m[4], m[8], m[12], + m[1], m[5], m[9], m[13], + m[2], m[6], m[10], m[14], + m[3], m[7], m[11], m[15]}; + + memcpy(m, t, sizeof(t)); +} + +/** + * Inverts a 4x4 matrix. + * + * This function can currently handle only pure translation-rotation matrices. + * Read http://www.gamedev.net/community/forums/topic.asp?topic_id=425118 + * for an explanation. + */ +static void +invert(GLfloat *m) +{ + GLfloat t[16]; + identity(t); + + // Extract and invert the translation part 't'. The inverse of a + // translation matrix can be calculated by negating the translation + // coordinates. + t[12] = -m[12]; t[13] = -m[13]; t[14] = -m[14]; + + // Invert the rotation part 'r'. The inverse of a rotation matrix is + // equal to its transpose. + m[12] = m[13] = m[14] = 0; + transpose(m); + + // inv(m) = inv(r) * inv(t) + multiply(m, t); +} + +/** + * Calculate a perspective projection transformation. + * + * @param m the matrix to save the transformation in + * @param fovy the field of view in the y direction + * @param aspect the view aspect ratio + * @param zNear the near clipping plane + * @param zFar the far clipping plane + */ +void perspective(GLfloat *m, GLfloat fovy, GLfloat aspect, GLfloat zNear, GLfloat zFar) +{ + GLfloat tmp[16]; + identity(tmp); + + double sine, cosine, cotangent, deltaZ; + GLfloat radians = fovy / 2 * M_PI / 180; + + deltaZ = zFar - zNear; + sincos(radians, &sine, &cosine); + + if ((deltaZ == 0) || (sine == 0) || (aspect == 0)) + return; + + cotangent = cosine / sine; + + tmp[0] = cotangent / aspect; + tmp[5] = cotangent; + tmp[10] = -(zFar + zNear) / deltaZ; + tmp[11] = -1; + tmp[14] = -2 * zNear * zFar / deltaZ; + tmp[15] = 0; + + memcpy(m, tmp, sizeof(tmp)); +} + +/** + * Draws a gear. + * + * @param gear the gear to draw + * @param transform the current transformation matrix + * @param x the x position to draw the gear at + * @param y the y position to draw the gear at + * @param angle the rotation angle of the gear + * @param color the color of the gear + */ +static void +draw_gear(struct gear *gear, GLfloat *transform, + GLfloat x, GLfloat y, GLfloat angle, const GLfloat color[4]) +{ + GLfloat model_view[16]; + GLfloat normal_matrix[16]; + GLfloat model_view_projection[16]; + + /* Translate and rotate the gear */ + memcpy(model_view, transform, sizeof (model_view)); + translate(model_view, x, y, 0); + rotate(model_view, 2 * M_PI * angle / 360.0, 0, 0, 1); + + /* Create and set the ModelViewProjectionMatrix */ + memcpy(model_view_projection, ProjectionMatrix, sizeof(model_view_projection)); + multiply(model_view_projection, model_view); + + glUniformMatrix4fv(ModelViewProjectionMatrix_location, 1, GL_FALSE, + model_view_projection); + + /* + * Create and set the NormalMatrix. It's the inverse transpose of the + * ModelView matrix. + */ + memcpy(normal_matrix, model_view, sizeof (normal_matrix)); + invert(normal_matrix); + transpose(normal_matrix); + glUniformMatrix4fv(NormalMatrix_location, 1, GL_FALSE, normal_matrix); + + /* Set the gear color */ + glUniform4fv(MaterialColor_location, 1, color); + + /* Set the vertex buffer object to use */ + glBindBuffer(GL_ARRAY_BUFFER, 0); + + /* Set up the position of the attributes in the vertex buffer object */ + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, + 6 * sizeof(GLfloat), *gear->vertices); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, + 6 * sizeof(GLfloat), ((float*)*gear->vertices) + 3*4); + /* Enable the attributes */ + glEnableVertexAttribArray(0); + glEnableVertexAttribArray(1); + + /* Draw the triangle strips that comprise the gear */ + int n; + for (n = 0; n < gear->nstrips; n++) + glDrawArrays(GL_TRIANGLE_STRIP, gear->strips[n].first, gear->strips[n].count); + + /* Disable the attributes */ + glDisableVertexAttribArray(1); + glDisableVertexAttribArray(0); +} + +/** + * Draws the gears. + */ +static void +gears_draw(void) +{ + const static GLfloat red[4] = { 0.8, 0.1, 0.0, 1.0 }; + const static GLfloat green[4] = { 0.0, 0.8, 0.2, 1.0 }; + const static GLfloat blue[4] = { 0.2, 0.2, 1.0, 1.0 }; + GLfloat transform[16]; + identity(transform); + + glClearColor(0.0, 0.0, 0.0, 0.0); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + /* Translate and rotate the view */ + translate(transform, 0, 0, -20); + rotate(transform, 2 * M_PI * view_rot[0] / 360.0, 1, 0, 0); + rotate(transform, 2 * M_PI * view_rot[1] / 360.0, 0, 1, 0); + rotate(transform, 2 * M_PI * view_rot[2] / 360.0, 0, 0, 1); + + /* Draw the gears */ + draw_gear(gear1, transform, -3.0, -2.0, angle, red); + draw_gear(gear2, transform, 3.1, -2.0, -2 * angle - 9.0, green); + draw_gear(gear3, transform, -3.1, 4.2, -2 * angle - 25.0, blue); + + glutSwapBuffers(); +} + +/** + * Handles a new window size or exposure. + * + * @param width the window width + * @param height the window height + */ +static void +gears_reshape(int width, int height) +{ + /* Update the projection matrix */ + perspective(ProjectionMatrix, 60.0, width / (float)height, 1.0, 1024.0); + + /* Set the viewport */ + glViewport(0, 0, (GLint) width, (GLint) height); +} + +/** + * Handles special glut events. + * + * @param special the event to handle. + */ +static void +gears_special(int special, int crap, int morecrap) +{ + switch (special) { + case GLUT_KEY_LEFT: + view_rot[1] += 5.0; + break; + case GLUT_KEY_RIGHT: + view_rot[1] -= 5.0; + break; + case GLUT_KEY_UP: + view_rot[0] += 5.0; + break; + case GLUT_KEY_DOWN: + view_rot[0] -= 5.0; + break; + case GLUT_KEY_F11: + glutFullScreen(); + break; + } +} + +static void +gears_idle(void) +{ + static int frames = 0; + static double tRot0 = -1.0, tRate0 = -1.0; + double dt, t = glutGet(GLUT_ELAPSED_TIME) / 1000.0; + + if (tRot0 < 0.0) + tRot0 = t; + dt = t - tRot0; + tRot0 = t; + + /* advance rotation for next frame */ + angle += 70.0 * dt; /* 70 degrees per second */ + if (angle > 3600.0) + angle -= 3600.0; + + glutPostRedisplay(); + frames++; + + if (tRate0 < 0.0) + tRate0 = t; + if (t - tRate0 >= 5.0) { + GLfloat seconds = t - tRate0; + GLfloat fps = frames / seconds; + printf("%d frames in %3.1f seconds = %6.3f FPS\n", frames, seconds, + fps); + tRate0 = t; + frames = 0; + } +} + +static const char vertex_shader[] = +"attribute vec3 position;\n" +"attribute vec3 normal;\n" +"\n" +"uniform mat4 ModelViewProjectionMatrix;\n" +"uniform mat4 NormalMatrix;\n" +"uniform vec4 LightSourcePosition;\n" +"uniform vec4 MaterialColor;\n" +"\n" +"varying vec4 Color;\n" +"\n" +"void main(void)\n" +"{\n" +" // Transform the normal to eye coordinates\n" +" vec3 N = normalize(vec3(NormalMatrix * vec4(normal, 1.0)));\n" +"\n" +" // The LightSourcePosition is actually its direction for directional light\n" +" vec3 L = normalize(LightSourcePosition.xyz);\n" +"\n" +" // Multiply the diffuse value by the vertex color (which is fixed in this case)\n" +" // to get the actual color that we will use to draw this vertex with\n" +" float diffuse = max(dot(N, L), 0.0);\n" +" Color = diffuse * MaterialColor;\n" +"\n" +" // Transform the position to clip coordinates\n" +" gl_Position = ModelViewProjectionMatrix * vec4(position, 1.0);\n" +"}"; + +static const char fragment_shader[] = +"#ifdef GL_ES\n" +"precision mediump float;\n" +"#endif\n" +"varying vec4 Color;\n" +"\n" +"void main(void)\n" +"{\n" +" gl_FragColor = Color;\n" +"}"; + +static void +gears_init(void) +{ + GLuint v, f, program; + const char *p; + char msg[512]; + + glEnable(GL_CULL_FACE); + glEnable(GL_DEPTH_TEST); + + /* Compile the vertex shader */ + p = vertex_shader; + v = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(v, 1, &p, NULL); + glCompileShader(v); + glGetShaderInfoLog(v, sizeof msg, NULL, msg); + printf("vertex shader info: %s\n", msg); + + /* Compile the fragment shader */ + p = fragment_shader; + f = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(f, 1, &p, NULL); + glCompileShader(f); + glGetShaderInfoLog(f, sizeof msg, NULL, msg); + printf("fragment shader info: %s\n", msg); + + /* Create and link the shader program */ + program = glCreateProgram(); + glAttachShader(program, v); + glAttachShader(program, f); + glBindAttribLocation(program, 0, "position"); + glBindAttribLocation(program, 1, "normal"); + + glLinkProgram(program); + glGetProgramInfoLog(program, sizeof msg, NULL, msg); + printf("info: %s\n", msg); + + /* Enable the shaders */ + glUseProgram(program); + + /* Get the locations of the uniforms so we can access them */ + ModelViewProjectionMatrix_location = glGetUniformLocation(program, "ModelViewProjectionMatrix"); + NormalMatrix_location = glGetUniformLocation(program, "NormalMatrix"); + LightSourcePosition_location = glGetUniformLocation(program, "LightSourcePosition"); + MaterialColor_location = glGetUniformLocation(program, "MaterialColor"); + + /* Set the LightSourcePosition uniform which is constant throught the program */ + glUniform4fv(LightSourcePosition_location, 1, LightSourcePosition); + + /* make the gears */ + gear1 = create_gear(1.0, 4.0, 1.0, 20, 0.7); + gear2 = create_gear(0.5, 2.0, 2.0, 10, 0.7); + gear3 = create_gear(1.3, 2.0, 0.5, 10, 0.7); +} + +int +main(int argc, char *argv[]) +{ + /* Initialize the window */ + glutInit(&argc, argv); + glutInitWindowSize(300, 300); + glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); + + glutCreateWindow("es2gears"); + + /* Set up glut callback functions */ + glutIdleFunc (gears_idle); + glutReshapeFunc(gears_reshape); + glutDisplayFunc(gears_draw); + glutSpecialFunc(gears_special); + + /* Initialize the gears */ + gears_init(); + + glutMainLoop(); + + return 0; +} diff --git a/tests/perspective.c b/tests/perspective.c new file mode 100644 index 00000000..72f4c50f --- /dev/null +++ b/tests/perspective.c @@ -0,0 +1,408 @@ +/* + * SDL OpenGL Tutorial. + * (c) Michael Vance, 2000 + * briareos@lokigames.com + * + * Distributed under terms of the LGPL. + */ + +#include <SDL/SDL.h> + +#ifdef EMSCRIPTEN +#include <GL/gl.h> +#include <GL/glu.h> +#include "emscripten.h" +#else +#include <GL/gl.h> +#include <GL/glu.h> +#endif +#include <stdio.h> +#include <stdlib.h> + +#ifdef EMSCRIPTEN + #define emColor4ubv(x) +#else +#define emColor4ubv(x) glColor4ubv(x) +#endif + +static GLboolean should_rotate = GL_TRUE; + +static void quit_tutorial( int code ) +{ + /* + * Quit SDL so we can release the fullscreen + * mode and restore the previous video settings, + * etc. + */ + SDL_Quit( ); + + /* Exit program. */ + exit( code ); +} + +static void handle_key_down( SDL_keysym* keysym ) +{ + + /* + * We're only interested if 'Esc' has + * been presssed. + * + * EXERCISE: + * Handle the arrow keys and have that change the + * viewing position/angle. + */ + switch( keysym->sym ) { + case SDLK_ESCAPE: + quit_tutorial( 0 ); + break; + case SDLK_SPACE: + should_rotate = !should_rotate; + break; + default: + break; + } + +} + +static void process_events( void ) +{ + /* Our SDL event placeholder. */ + SDL_Event event; + + /* Grab all the events off the queue. */ + while( SDL_PollEvent( &event ) ) { + + switch( event.type ) { + case SDL_KEYDOWN: + /* Handle key presses. */ + handle_key_down( &event.key.keysym ); + break; + case SDL_QUIT: + /* Handle quit requests (like Ctrl-c). */ + quit_tutorial( 0 ); + break; + } + + } + +} + +static void draw_screen( void ) +{ + /* Our angle of rotation. */ + static float angle = 0.0f; + + /* + * EXERCISE: + * Replace this awful mess with vertex + * arrays and a call to glDrawElements. + * + * EXERCISE: + * After completing the above, change + * it to use compiled vertex arrays. + * + * EXERCISE: + * Verify my windings are correct here ;). + */ + static GLfloat v0[] = { -1.0f, -1.0f, 1.0f }; + static GLfloat v1[] = { 1.0f, -1.0f, 1.0f }; + static GLfloat v2[] = { 1.0f, 1.0f, 1.0f }; + static GLfloat v3[] = { -1.0f, 1.0f, 1.0f }; + static GLfloat v4[] = { -1.0f, -1.0f, -1.0f }; + static GLfloat v5[] = { 1.0f, -1.0f, -1.0f }; + static GLfloat v6[] = { 1.0f, 1.0f, -1.0f }; + static GLfloat v7[] = { -1.0f, 1.0f, -1.0f }; + static GLubyte red[] = { 255, 0, 0, 255 }; + static GLubyte green[] = { 0, 255, 0, 255 }; + static GLubyte blue[] = { 0, 0, 255, 255 }; + static GLubyte white[] = { 255, 255, 255, 255 }; + static GLubyte yellow[] = { 0, 255, 255, 255 }; + static GLubyte black[] = { 0, 0, 0, 255 }; + static GLubyte orange[] = { 255, 255, 0, 255 }; + static GLubyte purple[] = { 255, 0, 255, 0 }; + + /* Clear the color and depth buffers. */ + glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); + + /* We don't want to modify the projection matrix. */ + glMatrixMode( GL_MODELVIEW ); + glLoadIdentity( ); + + /* Move down the z-axis. */ + glTranslatef( 0.0, 0.0, -5.0 ); + + /* Rotate. */ + glRotatef( angle, 0.0, 1.0, 0.0 ); + + if( should_rotate ) { + + if( ++angle > 360.0f ) { + angle = 0.0f; + } + + } + + /* Send our triangle data to the pipeline. */ + glBegin( GL_TRIANGLES ); + + emColor4ubv( red ); + glVertex3fv( v0 ); + emColor4ubv( green ); + glVertex3fv( v1 ); + emColor4ubv( blue ); + glVertex3fv( v2 ); + + emColor4ubv( red ); + glVertex3fv( v0 ); + emColor4ubv( blue ); + glVertex3fv( v2 ); + emColor4ubv( white ); + glVertex3fv( v3 ); + + emColor4ubv( green ); + glVertex3fv( v1 ); + emColor4ubv( black ); + glVertex3fv( v5 ); + emColor4ubv( orange ); + glVertex3fv( v6 ); + + emColor4ubv( green ); + glVertex3fv( v1 ); + emColor4ubv( orange ); + glVertex3fv( v6 ); + emColor4ubv( blue ); + glVertex3fv( v2 ); + + emColor4ubv( black ); + glVertex3fv( v5 ); + emColor4ubv( yellow ); + glVertex3fv( v4 ); + emColor4ubv( purple ); + glVertex3fv( v7 ); + + emColor4ubv( black ); + glVertex3fv( v5 ); + emColor4ubv( purple ); + glVertex3fv( v7 ); + emColor4ubv( orange ); + glVertex3fv( v6 ); + + emColor4ubv( yellow ); + glVertex3fv( v4 ); + emColor4ubv( red ); + glVertex3fv( v0 ); + emColor4ubv( white ); + glVertex3fv( v3 ); + + emColor4ubv( yellow ); + glVertex3fv( v4 ); + emColor4ubv( white ); + glVertex3fv( v3 ); + emColor4ubv( purple ); + glVertex3fv( v7 ); + + emColor4ubv( white ); + glVertex3fv( v3 ); + emColor4ubv( blue ); + glVertex3fv( v2 ); + emColor4ubv( orange ); + glVertex3fv( v6 ); + + emColor4ubv( white ); + glVertex3fv( v3 ); + emColor4ubv( orange ); + glVertex3fv( v6 ); + emColor4ubv( purple ); + glVertex3fv( v7 ); + + emColor4ubv( green ); + glVertex3fv( v1 ); + emColor4ubv( red ); + glVertex3fv( v0 ); + emColor4ubv( yellow ); + glVertex3fv( v4 ); + + emColor4ubv( green ); + glVertex3fv( v1 ); + emColor4ubv( yellow ); + glVertex3fv( v4 ); + emColor4ubv( black ); + glVertex3fv( v5 ); + + glEnd( ); + + /* + * EXERCISE: + * Draw text telling the user that 'Spc' + * pauses the rotation and 'Esc' quits. + * Do it using vetors and textured quads. + */ + + /* + * Swap the buffers. This this tells the driver to + * render the next frame from the contents of the + * back-buffer, and to set all rendering operations + * to occur on what was the front-buffer. + * + * Double buffering prevents nasty visual tearing + * from the application drawing on areas of the + * screen that are being updated at the same time. + */ + SDL_GL_SwapBuffers( ); +} + +static void setup_opengl( int width, int height ) +{ + float ratio = (float) width / (float) height; + + /* Our shading model--Gouraud (smooth). */ + glShadeModel( GL_SMOOTH ); + + /* Culling. */ + glCullFace( GL_BACK ); + glFrontFace( GL_CCW ); + glEnable( GL_CULL_FACE ); + + /* Set the clear color. */ + glClearColor( 0, 0, 0, 0 ); + + /* Setup our viewport. */ + glViewport( 0, 0, width, height ); + + /* + * Change to the projection matrix and set + * our viewing volume. + */ + glMatrixMode( GL_PROJECTION ); + glLoadIdentity( ); + /* + * EXERCISE: + * Replace this with a call to glFrustum. + */ + gluPerspective( 60.0, ratio, 1.0, 1024.0 ); +} + +void one_iter(); +void one_iter() { + process_events( ); + /* Draw the screen. */ + draw_screen( ); +} + +int main( int argc, char* argv[] ) +{ + /* Information about the current video settings. */ + const SDL_VideoInfo* info = NULL; + /* Dimensions of our window. */ + int width = 0; + int height = 0; + /* Color depth in bits of our window. */ + int bpp = 0; + /* Flags we will pass into SDL_SetVideoMode. */ + int flags = 0; + + /* First, initialize SDL's video subsystem. */ + if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) { + /* Failed, exit. */ + fprintf( stderr, "Video initialization failed: %s\n", + SDL_GetError( ) ); + quit_tutorial( 1 ); + } + + /* Let's get some video information. */ + info = SDL_GetVideoInfo( ); + + if( !info ) { + /* This should probably never happen. */ + fprintf( stderr, "Video query failed: %s\n", + SDL_GetError( ) ); + quit_tutorial( 1 ); + } + + /* + * Set our width/height to 640/480 (you would + * of course let the user decide this in a normal + * app). We get the bpp we will request from + * the display. On X11, VidMode can't change + * resolution, so this is probably being overly + * safe. Under Win32, ChangeDisplaySettings + * can change the bpp. + */ + width = 640; + height = 480; + bpp = info->vfmt->BitsPerPixel; + + /* + * Now, we want to setup our requested + * window attributes for our OpenGL window. + * We want *at least* 5 bits of red, green + * and blue. We also want at least a 16-bit + * depth buffer. + * + * The last thing we do is request a double + * buffered window. '1' turns on double + * buffering, '0' turns it off. + * + * Note that we do not use SDL_DOUBLEBUF in + * the flags to SDL_SetVideoMode. That does + * not affect the GL attribute state, only + * the standard 2D blitting setup. + */ +// SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 ); +// SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 ); +// SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 ); +// SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 ); +// SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); + + /* + * We want to request that SDL provide us + * with an OpenGL window, in a fullscreen + * video mode. + * + * EXERCISE: + * Make starting windowed an option, and + * handle the resize events properly with + * glViewport. + */ + flags = SDL_OPENGL;// | SDL_FULLSCREEN; + + /* + * Set the video mode + */ + if( SDL_SetVideoMode( width, height, bpp, flags ) == 0 ) { + /* + * This could happen for a variety of reasons, + * including DISPLAY not being set, the specified + * resolution not being available, etc. + */ + fprintf( stderr, "Video mode set failed: %s\n", + SDL_GetError( ) ); + quit_tutorial( 1 ); + } + + /* + * At this point, we should have a properly setup + * double-buffered window for use with OpenGL. + */ + setup_opengl( width, height ); + + /* + * Now we want to begin our normal app process-- + * an event loop with a lot of redrawing. + */ + one_iter(); // just one for testing purposes + +#ifndef EMSCRIPTEN + SDL_Delay(2000); +#endif + + /* + * EXERCISE: + * Record timings using SDL_GetTicks() and + * and print out frames per second at program + * end. + */ + + /* Never reached. */ + return 0; +} diff --git a/tests/perspective.png b/tests/perspective.png Binary files differnew file mode 100644 index 00000000..04cedc71 --- /dev/null +++ b/tests/perspective.png diff --git a/tests/runner.py b/tests/runner.py index b6980933..1f40d69c 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -100,14 +100,8 @@ class RunnerCore(unittest.TestCase): for temp_file in os.listdir(TEMP_DIR): if temp_file.endswith('.ll'): self.has_prev_ll = True - + def tearDown(self): - if self.save_JS: - for name in os.listdir(self.get_dir()): - if name.endswith(('.o.js', '.cc.js')): - suff = '.'.join(name.split('.')[-2:]) - shutil.copy(os.path.join(self.get_dir(), name), - os.path.join(TEMP_DIR, self.id().replace('__main__.', '').replace('.test_', '.')+'.'+suff)) if not self.save_dir: # rmtree() fails on Windows if the current working directory is inside the tree. os.chdir(os.path.join(self.get_dir(), '..')) @@ -140,6 +134,12 @@ class RunnerCore(unittest.TestCase): def get_stdout_path(self): return os.path.join(self.get_dir(), 'stdout') + def hardcode_arguments(self, filename, args): + # Hardcode in the arguments, so js is portable without manual commandlinearguments + if not args: return + js = open(filename).read() + open(filename, 'w').write(js.replace('var ret = run();', 'var ret = run(%s);' % str(args))) + def prep_ll_run(self, filename, ll_file, force_recompile=False, build_ll_hook=None): if ll_file.endswith(('.bc', '.o')): if ll_file != filename + '.o': @@ -435,6 +435,8 @@ process(sys.argv[1]) sys.argv = map(lambda arg: arg if not arg.startswith('test_') else 'default.' + arg, sys.argv) +test_index = 0 + if 'benchmark' not in str(sys.argv) and 'sanity' not in str(sys.argv) and 'browser' not in str(sys.argv): # Tests @@ -448,29 +450,35 @@ if 'benchmark' not in str(sys.argv) and 'sanity' not in str(sys.argv) and 'brows class T(RunnerCore): # Short name, to make it more fun to use manually on the commandline ## Does a complete test - builds, runs, checks output, etc. def do_run(self, src, expected_output, args=[], output_nicerizer=None, output_processor=None, no_build=False, main_file=None, additional_files=[], js_engines=None, post_build=None, basename='src.cpp', libraries=[], includes=[], force_c=False, build_ll_hook=None, extra_emscripten_args=[]): - if force_c or (main_file is not None and main_file[-2:]) == '.c': - basename = 'src.c' - Building.COMPILER = to_cc(Building.COMPILER) - - dirname = self.get_dir() - filename = os.path.join(dirname, basename) - if not no_build: - self.build(src, dirname, filename, main_file=main_file, additional_files=additional_files, libraries=libraries, includes=includes, - build_ll_hook=build_ll_hook, extra_emscripten_args=extra_emscripten_args, post_build=post_build) - - # Run in both JavaScript engines, if optimizing - significant differences there (typed arrays) - if js_engines is None: - js_engines = JS_ENGINES - if Settings.USE_TYPED_ARRAYS: - js_engines = filter(lambda engine: engine != V8_ENGINE, js_engines) # V8 issue 1822 - js_engines = filter(lambda engine: engine not in self.banned_js_engines, js_engines) - if len(js_engines) == 0: return self.skip('No JS engine present to run this test with. Check %s and the paths therein.' % EM_CONFIG) - for engine in js_engines: - js_output = self.run_generated_code(engine, filename + '.o.js', args, output_nicerizer=output_nicerizer) - self.assertContained(expected_output, js_output.replace('\r\n', '\n')) - self.assertNotContained('ERROR', js_output) - - #shutil.rmtree(dirname) # TODO: leave no trace in memory. But for now nice for debugging + if force_c or (main_file is not None and main_file[-2:]) == '.c': + basename = 'src.c' + Building.COMPILER = to_cc(Building.COMPILER) + + dirname = self.get_dir() + filename = os.path.join(dirname, basename) + if not no_build: + self.build(src, dirname, filename, main_file=main_file, additional_files=additional_files, libraries=libraries, includes=includes, + build_ll_hook=build_ll_hook, extra_emscripten_args=extra_emscripten_args, post_build=post_build) + + # Run in both JavaScript engines, if optimizing - significant differences there (typed arrays) + if js_engines is None: + js_engines = JS_ENGINES + if Settings.USE_TYPED_ARRAYS: + js_engines = filter(lambda engine: engine != V8_ENGINE, js_engines) # V8 issue 1822 + js_engines = filter(lambda engine: engine not in self.banned_js_engines, js_engines) + if len(js_engines) == 0: return self.skip('No JS engine present to run this test with. Check %s and the paths therein.' % EM_CONFIG) + for engine in js_engines: + js_output = self.run_generated_code(engine, filename + '.o.js', args, output_nicerizer=output_nicerizer) + self.assertContained(expected_output, js_output.replace('\r\n', '\n')) + self.assertNotContained('ERROR', js_output) + + #shutil.rmtree(dirname) # TODO: leave no trace in memory. But for now nice for debugging + + if self.save_JS: + global test_index + self.hardcode_arguments(filename + '.o.js', args) + shutil.copyfile(filename + '.o.js', os.path.join(TEMP_DIR, str(test_index) + '.js')) + test_index += 1 # No building - just process an existing .ll file (or .bc, which we turn into .ll) def do_ll_run(self, ll_file, expected_output=None, args=[], js_engines=None, output_nicerizer=None, post_build=None, force_recompile=False, build_ll_hook=None, extra_emscripten_args=[]): @@ -499,6 +507,8 @@ if 'benchmark' not in str(sys.argv) and 'sanity' not in str(sys.argv) and 'brows ''' self.do_run(src, 'hello, world!') + assert 'EMSCRIPTEN_GENERATED_FUNCTIONS' not in open(self.in_dir('src.cpp.o.js')).read(), 'must not emit this unneeded internal thing' + def test_intvars(self): if self.emcc_args == None: return self.skip('needs ta2') @@ -1044,6 +1054,19 @@ m_divisor is 1091269979 } ''', 'c = 4ca38a6bd2973f97') + def test_i64_llabs(self): + if Settings.USE_TYPED_ARRAYS != 2: return self.skip('full i64 stuff only in ta2') + Settings.PRECISE_I64_MATH = 2 + self.do_run(r''' + #include <stdio.h> + #include <stdlib.h> + + int main(int argc, char ** argv) { + printf("%lld,%lld\n", llabs(-576460752303423489), llabs(576460752303423489)); + return 0; + } + ''', '576460752303423489,576460752303423489') + def test_i64_zextneg(self): if Settings.USE_TYPED_ARRAYS != 2: return self.skip('full i64 stuff only in ta2') @@ -1131,7 +1154,6 @@ m_divisor is 1091269979 def test_i32_mul_precise(self): if self.emcc_args == None: return self.skip('needs ta2') - self.emcc_args += ['-s', 'PRECISE_I32_MUL=1'] src = r''' #include <stdio.h> @@ -1146,6 +1168,10 @@ m_divisor is 1091269979 self.do_run(src, '3217489085') def test_i32_mul_semiprecise(self): + if Settings.ASM_JS: return self.skip('asm is always fully precise') + + Settings.PRECISE_I32_MUL = 0 # we want semiprecise here + src = r''' #include <stdio.h> @@ -1227,6 +1253,8 @@ m_divisor is 1091269979 extern int64_t llvm_ctlz_i64(int64_t x); extern int32_t llvm_cttz_i32(int32_t x); extern int64_t llvm_cttz_i64(int64_t x); + extern int32_t llvm_ctpop_i32(int32_t x); + extern int64_t llvm_ctpop_i64(int64_t x); extern int llvm_expect_i32(int x, int y); } @@ -1243,6 +1271,7 @@ m_divisor is 1091269979 printf("%d,%d\n", (int)llvm_ctlz_i64(((int64_t)1) << 40), llvm_ctlz_i32(1<<10)); printf("%d,%d\n", (int)llvm_cttz_i64(((int64_t)1) << 40), llvm_cttz_i32(1<<10)); + printf("%d,%d\n", (int)llvm_ctpop_i64((0x3101ULL << 32) | 1), llvm_ctpop_i32(0x3101)); printf("%d\n", llvm_expect_i32(x % 27, 3)); @@ -1259,6 +1288,7 @@ c8,ef c5,de,15,8a 23,21 40,10 +5,4 13 72057594037927936 ''') @@ -1807,6 +1837,8 @@ Succeeded! generated = open('src.cpp.o.js', 'r').read() def test_stack(self): + Settings.INLINING_LIMIT = 50 + src = ''' #include <stdio.h> int test(int i) { @@ -2457,12 +2489,15 @@ Exception execution path of first function! 1 ''' Settings.DISABLE_EXCEPTION_CATCHING = 0 + if '-O2' in self.emcc_args: + self.emcc_args.pop() ; self.emcc_args.pop() # disable closure to work around a closure bug self.do_run(src, 'Throw...Construct...Catched...Destruct...Throw...Construct...Copy...Catched...Destruct...Destruct...') def test_white_list_exception(self): if Settings.ASM_JS: return self.skip('no exceptions support in asm') Settings.DISABLE_EXCEPTION_CATCHING = 2 Settings.EXCEPTION_CATCHING_WHITELIST = ["__Z12somefunctionv"] + Settings.INLINING_LIMIT = 50 # otherwise it is inlined and not identified src = ''' #include <stdio.h> @@ -2495,8 +2530,6 @@ Exception execution path of first function! 1 def test_uncaught_exception(self): if Settings.ASM_JS: return self.skip('no exceptions support in asm') if self.emcc_args is None: return self.skip('no libcxx inclusion without emcc') - if '-O2' in self.emcc_args: - self.emcc_args += ['--closure', '1'] # Use closure here for some additional coverage Settings.DISABLE_EXCEPTION_CATCHING = 0 @@ -2609,6 +2642,7 @@ Exiting setjmp function, level: 0, prev_jmp: -1 if self.emcc_args is None: return self.skip('requires emcc') if Settings.ASM_JS: return self.skip('uses report_stack without exporting') + Settings.INLINING_LIMIT = 50 Settings.CATCH_EXIT_CODE = 1 src = r''' @@ -2793,6 +2827,37 @@ Exiting setjmp function, level: 0, prev_jmp: -1 ''' % addr self.do_run(src, 'segmentation fault' if addr.isdigit() else 'marfoosh') + def test_safe_dyncalls(self): + if Settings.ASM_JS: return self.skip('asm does not support missing function stack traces') + if Settings.SAFE_HEAP: return self.skip('safe heap warning will appear instead') + if self.emcc_args is None: return self.skip('need libc') + + Settings.SAFE_DYNCALLS = 1 + + for cond, body, work in [(True, True, False), (True, False, False), (False, True, True), (False, False, False)]: + print cond, body, work + src = r''' + #include <stdio.h> + + struct Classey { + virtual void doIt() = 0; + }; + + struct D1 : Classey { + virtual void doIt() BODY; + }; + + int main(int argc, char **argv) + { + Classey *p = argc COND 100 ? new D1() : NULL; + printf("%p\n", p); + p->doIt(); + + return 0; + } + '''.replace('COND', '==' if cond else '!=').replace('BODY', r'{ printf("all good\n"); }' if body else '') + self.do_run(src, 'dyncall error: vi' if not work else 'all good') + def test_dynamic_cast(self): if self.emcc_args is None: return self.skip('need libcxxabi') @@ -2845,6 +2910,23 @@ Exiting setjmp function, level: 0, prev_jmp: -1 ''' self.do_run(src, 'a1: 0\na2: 0\na3: 1\nb1: 0\nb2: 1\nb3: 1\nc1: 1\nc2: 1\nc3: 1\n') + def test_dynamic_cast_2(self): + if self.emcc_args is None: return self.skip('need libcxxabi') + + src = r''' + #include <stdio.h> + #include <typeinfo> + + class Class {}; + + int main() { + const Class* dp = dynamic_cast<const Class*>(&typeid(Class)); + // should return dp == NULL, + printf("pointer: %p\n", dp); + } + ''' + self.do_run(src, "pointer: (nil)") + def test_funcptr(self): src = ''' #include <stdio.h> @@ -3008,6 +3090,8 @@ Exiting setjmp function, level: 0, prev_jmp: -1 def test_stack_varargs(self): if self.emcc_args is None: return # too slow in other modes + Settings.INLINING_LIMIT = 50 + # We should not blow up the stack with numerous varargs src = r''' #include <stdio.h> @@ -3028,6 +3112,8 @@ Exiting setjmp function, level: 0, prev_jmp: -1 self.do_run(src, 'ok!') def test_stack_void(self): + Settings.INLINING_LIMIT = 50 + src = r''' #include <stdio.h> @@ -3742,6 +3828,8 @@ The current type of b is: 9 ''') def test_structbyval(self): + Settings.INLINING_LIMIT = 50 + # part 1: make sure that normally, passing structs by value works src = r''' @@ -3966,8 +4054,19 @@ The current type of b is: 9 ''' self.do_run(src, '*0\n') + def test_time_c(self): + src = r''' + #include <time.h> + #include <stdio.h> + + int main() { + time_t t = time(0); + printf("time: %s\n", ctime(&t)); + } + ''' + self.do_run(src, 'time: ') # compilation check, mainly + def test_intentional_fault(self): - if Settings.ASM_JS: return self.skip('no throw support in asm') # Some programs intentionally segfault themselves, we should compile that into a throw src = r''' int main () { @@ -3975,7 +4074,7 @@ The current type of b is: 9 return 0; } ''' - self.do_run(src, 'fault on write to 0') + self.do_run(src, 'fault on write to 0' if not Settings.ASM_JS else 'Assertion: 0') def test_trickystring(self): src = r''' @@ -4095,10 +4194,12 @@ The current type of b is: 9 ''' self.do_run(src, '*0.00,0.00,0.00*\n*0,77,0*\n*0,77,0*\n*0,77,0*') - def test_memcpy(self): + def test_memcpy_memcmp(self): src = ''' #include <stdio.h> #include <string.h> + #include <assert.h> + #define MAXX 48 void reset(unsigned char *buffer) { for (int i = 0; i < MAXX; i++) buffer[i] = i+1; @@ -4119,6 +4220,20 @@ The current type of b is: 9 reset(buffer); memcpy(buffer+i, buffer+j, k); dump(buffer); + assert(memcmp(buffer+i, buffer+j, k) == 0); + buffer[i + k/2]++; + if (buffer[i + k/2] != 0) { + assert(memcmp(buffer+i, buffer+j, k) > 0); + } else { + assert(memcmp(buffer+i, buffer+j, k) < 0); + } + buffer[i + k/2]--; + buffer[j + k/2]++; + if (buffer[j + k/2] != 0) { + assert(memcmp(buffer+i, buffer+j, k) < 0); + } else { + assert(memcmp(buffer+i, buffer+j, k) > 0); + } } } } @@ -4155,6 +4270,139 @@ The current type of b is: 9 ''' self.do_run(src, 'ok.'); + def test_getopt(self): + src = ''' + #pragma clang diagnostic ignored "-Winvalid-pp-token" + #include <unistd.h> + #include <stdlib.h> + #include <stdio.h> + + int + main(int argc, char *argv[]) + { + int flags, opt; + int nsecs, tfnd; + + nsecs = 0; + tfnd = 0; + flags = 0; + while ((opt = getopt(argc, argv, "nt:")) != -1) { + switch (opt) { + case 'n': + flags = 1; + break; + case 't': + nsecs = atoi(optarg); + tfnd = 1; + break; + default: /* '?' */ + fprintf(stderr, "Usage: %s [-t nsecs] [-n] name\\n", + argv[0]); + exit(EXIT_FAILURE); + } + } + + printf("flags=%d; tfnd=%d; optind=%d\\n", flags, tfnd, optind); + + if (optind >= argc) { + fprintf(stderr, "Expected argument after options\\n"); + exit(EXIT_FAILURE); + } + + printf("name argument = %s\\n", argv[optind]); + + /* Other code omitted */ + + exit(EXIT_SUCCESS); + } + ''' + self.do_run(src, 'flags=1; tfnd=1; optind=4\nname argument = foobar', args=['-t', '12', '-n', 'foobar']) + + def test_getopt_long(self): + src = ''' + #pragma clang diagnostic ignored "-Winvalid-pp-token" + #pragma clang diagnostic ignored "-Wdeprecated-writable-strings" + #include <stdio.h> /* for printf */ + #include <stdlib.h> /* for exit */ + #include <getopt.h> + + int + main(int argc, char **argv) + { + int c; + int digit_optind = 0; + + while (1) { + int this_option_optind = optind ? optind : 1; + int option_index = 0; + static struct option long_options[] = { + {"add", required_argument, 0, 0 }, + {"append", no_argument, 0, 0 }, + {"delete", required_argument, 0, 0 }, + {"verbose", no_argument, 0, 0 }, + {"create", required_argument, 0, 'c'}, + {"file", required_argument, 0, 0 }, + {0, 0, 0, 0 } + }; + + c = getopt_long(argc, argv, "abc:d:012", + long_options, &option_index); + if (c == -1) + break; + + switch (c) { + case 0: + printf("option %s", long_options[option_index].name); + if (optarg) + printf(" with arg %s", optarg); + printf("\\n"); + break; + + case '0': + case '1': + case '2': + if (digit_optind != 0 && digit_optind != this_option_optind) + printf("digits occur in two different argv-elements.\\n"); + digit_optind = this_option_optind; + printf("option %c\\n", c); + break; + + case 'a': + printf("option a\\n"); + break; + + case 'b': + printf("option b\\n"); + break; + + case 'c': + printf("option c with value '%s'\\n", optarg); + break; + + case 'd': + printf("option d with value '%s'\\n", optarg); + break; + + case '?': + break; + + default: + printf("?? getopt returned character code 0%o ??\\n", c); + } + } + + if (optind < argc) { + printf("non-option ARGV-elements: "); + while (optind < argc) + printf("%s ", argv[optind++]); + printf("\\n"); + } + + exit(EXIT_SUCCESS); + } + ''' + self.do_run(src, 'option file with arg foobar\noption b', args=['--file', 'foobar', '-b']) + def test_memmove(self): src = ''' #include <stdio.h> @@ -4197,6 +4445,19 @@ The current type of b is: 9 ''' self.do_run(src, 'final: -403200.'); + def test_memmove3(self): + src = ''' + #include <stdio.h> + #include <string.h> + int main() { + char str[] = "memmove can be vvery useful....!"; + memmove(str+15, str+16, 17); + puts(str); + return 0; + } + ''' + self.do_run(src, 'memmove can be very useful....!') + def test_bsearch(self): if Settings.QUANTUM_SIZE == 1: return self.skip('Test cannot work with q1') @@ -4797,6 +5058,9 @@ def process(filename): printf("%g\n", strtod(str, &endptr)); printf("%d\n", endptr - str); printf("%g\n", strtod("84e+420", &endptr)); + + printf("%.12f\n", strtod("1.2345678900000000e+08", NULL)); + return 0; } ''' @@ -4824,6 +5088,7 @@ def process(filename): 1.234e+57 10 inf + 123456789.000000000000 ''' self.do_run(src, re.sub(r'\n\s+', '\n', expected)) @@ -4976,6 +5241,22 @@ at function.:blag } ''' self.do_run(src, '22 : me and myself 25 1.34\n21 waka 95\n') + + def test_perrar(self): + src = r''' + #include <sys/types.h> + #include <sys/stat.h> + #include <fcntl.h> + #include <stdio.h> + + int main( int argc, char** argv ){ + int retval = open( "NonExistingFile", O_RDONLY ); + if( retval == -1 ) + perror( "Cannot open NonExistingFile" ); + return 0; + } + ''' + self.do_run(src, 'Cannot open NonExistingFile: No such file or directory\n') def test_atoX(self): if self.emcc_args is None: return self.skip('requires ta2') @@ -5299,9 +5580,30 @@ Pass: 0.000012 0.000012''') return(0); } ''' - self.do_run(src, '3\nday 19, month Nov, year 2012'); + def test_sscanf_5(self): + src = r''' + #include "stdio.h" + + static const char *colors[] = { + " c black", + ". c #001100", + "X c #111100" + }; + + int main(){ + unsigned char code; + char color[32]; + int rcode; + for(int i = 0; i < 3; i++) { + rcode = sscanf(colors[i], "%c c %s", &code, color); + printf("%i, %c, %s\n", rcode, code, color); + } + } + ''' + self.do_run(src, '2, , black\n2, ., #001100\n2, X, #111100'); + def test_langinfo(self): src = open(path_from_root('tests', 'langinfo', 'test.c'), 'r').read() expected = open(path_from_root('tests', 'langinfo', 'output.txt'), 'r').read() @@ -6283,6 +6585,53 @@ int main(int argc, char **argv) { self.do_run(src, '789:123.46\n0:100.1') + def test_reinterpreted_ptrs(self): + if self.emcc_args is None: return self.skip('needs emcc and libc') + + src = r''' +#include <stdio.h> + +class Foo { +private: + float bar; +public: + int baz; + + Foo(): bar(0), baz(4711) {}; + + int getBar() const; +}; + +int Foo::getBar() const { + return this->bar; +}; + +const Foo *magic1 = reinterpret_cast<Foo*>(0xDEAD111F); +const Foo *magic2 = reinterpret_cast<Foo*>(0xDEAD888F); + +static void runTest() { + + const Foo *a = new Foo(); + const Foo *b = a; + + if (a->getBar() == 0) { + if (a->baz == 4712) + b = magic1; + else + b = magic2; + } + + printf("%s\n", (b == magic1 ? "magic1" : (b == magic2 ? "magic2" : "neither"))); +}; + +extern "C" { + int main(int argc, char **argv) { + runTest(); + } +} +''' + self.do_run(src, 'magic2') + def test_jansson(self): return self.skip('currently broken') @@ -6719,7 +7068,6 @@ void*:16 extra_emscripten_args=['-H', 'libc/fcntl.h,libc/sys/unistd.h,poll.h,libc/math.h,libc/langinfo.h,libc/time.h']) def get_freetype(self): - Settings.INIT_STACK = 1 # TODO: Investigate why this is necessary return self.get_library('freetype', os.path.join('objs', '.libs', 'libfreetype.a')) @@ -6782,8 +7130,9 @@ def process(filename): # gcc -O3 -I/home/alon/Dev/emscripten/tests/sqlite -ldl src.c if self.emcc_args is None: return self.skip('Very slow without ta2, and we would also need to include dlmalloc manually without emcc') if Settings.QUANTUM_SIZE == 1: return self.skip('TODO FIXME') + self.banned_js_engines = [NODE_JS] # OOM in older node - Settings.CORRECT_SIGNS = 1 # XXX: in default, we fail with 2 here, even though the pgo_data should be correct (and works in s_0_0). Investigate this. + Settings.CORRECT_SIGNS = 1 Settings.CORRECT_OVERFLOWS = 0 Settings.CORRECT_ROUNDINGS = 0 if self.emcc_args is None: Settings.SAFE_HEAP = 0 # uses time.h to set random bytes, other stuff @@ -6827,7 +7176,8 @@ def process(filename): self.do_run(open(path_from_root('tests', 'bullet', 'Demos', 'HelloWorld', 'HelloWorld.cpp'), 'r').read(), [open(path_from_root('tests', 'bullet', 'output.txt'), 'r').read(), # different roundings - open(path_from_root('tests', 'bullet', 'output2.txt'), 'r').read()], + open(path_from_root('tests', 'bullet', 'output2.txt'), 'r').read(), + open(path_from_root('tests', 'bullet', 'output3.txt'), 'r').read()], libraries=self.get_library('bullet', [os.path.join('src', '.libs', 'libBulletDynamics.a'), os.path.join('src', '.libs', 'libBulletCollision.a'), os.path.join('src', '.libs', 'libLinearMath.a')], @@ -6918,7 +7268,6 @@ def process(filename): [os.path.sep.join('codec/CMakeFiles/j2k_to_image.dir/index.c.o'.split('/')), os.path.sep.join('codec/CMakeFiles/j2k_to_image.dir/convert.c.o'.split('/')), os.path.sep.join('codec/CMakeFiles/j2k_to_image.dir/__/common/color.c.o'.split('/')), - os.path.sep.join('codec/CMakeFiles/j2k_to_image.dir/__/common/getopt.c.o'.split('/')), os.path.join('bin', self.get_shared_library_name('libopenjpeg.so.1.4.0'))], configure=['cmake', '.'], #configure_args=['--enable-tiff=no', '--enable-jp3d=no', '--enable-png=no'], @@ -6995,6 +7344,14 @@ def process(filename): self.assertIdentical(clean(open('release.js').read()), clean(open('debug%d.js' % debug).read())) # EMCC_DEBUG=1 mode must not generate different code! print >> sys.stderr, 'debug check %d passed too' % debug + try: + os.environ['EMCC_FORCE_STDLIBS'] = '1' + print 'EMCC_FORCE_STDLIBS' + do_test() + finally: + del os.environ['EMCC_FORCE_STDLIBS'] + print >> sys.stderr, 'EMCC_FORCE_STDLIBS ok' + try_delete(CANONICAL_TEMP_DIR) else: print >> sys.stderr, 'not doing debug check' @@ -7062,6 +7419,24 @@ def process(filename): finally: del os.environ['EMCC_LEAVE_INPUTS_RAW'] + def test_fuzz(self): + if Settings.USE_TYPED_ARRAYS != 2: return self.skip('needs ta2') + + Building.COMPILER_TEST_OPTS += ['-I' + path_from_root('tests', 'fuzz')] + + def run_all(x): + print x + for name in glob.glob(path_from_root('tests', 'fuzz', '*.c')): + print name + self.do_run(open(path_from_root('tests', 'fuzz', name)).read(), + open(path_from_root('tests', 'fuzz', name + '.txt')).read(), force_c=True) + + run_all('normal') + + self.emcc_args += ['--llvm-lto', '1'] + + run_all('lto') + # Autodebug the code def do_autodebug(self, filename): output = Popen([PYTHON, AUTODEBUGGER, filename+'.o.ll', filename+'.o.ll.ll'], stdout=PIPE, stderr=self.stderr_redirect).communicate()[0] @@ -7113,40 +7488,101 @@ def process(filename): ''' self.do_run(src, '''AD:-1,1''', build_ll_hook=self.do_autodebug) - def test_profiling(self): - if Settings.ASM_JS: return self.skip('asm does not support profiling') + def test_corruption(self): + if Settings.ASM_JS: return self.skip('cannot use corruption checks in asm') + if Settings.USE_TYPED_ARRAYS != 2: return self.skip('needs ta2 for actual test') - src = ''' - #include <emscripten.h> - #include <unistd.h> + Settings.CORRUPTION_CHECK = 1 - int main() + src = r''' + #include <stdio.h> + #include <stdlib.h> + #include <string.h> + int main(int argc, char **argv) { + int size = 1024*argc; + char *buffer = (char*)malloc(size); + #if CORRUPT + memset(buffer, argc, size+15); + #else + memset(buffer, argc, size); + #endif + for (int x = 0; x < size; x += argc*3) buffer[x] = x/3; + int ret = 0; + for (int x = 0; x < size; x++) ret += buffer[x]; + free(buffer); + printf("All ok, %d\n", ret); + } + ''' + + for corrupt in [1]: + self.do_run(src.replace('CORRUPT', str(corrupt)), 'Heap corruption detected!' if corrupt else 'All ok, 4209') + + def test_corruption_2(self): + if Settings.ASM_JS: return self.skip('cannot use corruption checks in asm') + if Settings.USE_TYPED_ARRAYS != 2: return self.skip('needs ta2 for actual test') + + Settings.SAFE_HEAP = 1 + Settings.CORRUPTION_CHECK = 1 + + # test for free(0), malloc(0), etc. + src = r''' + #include <iostream> + #include <fstream> + #include <stdlib.h> + #include <stdio.h> + + void bye() { + printf("all ok\n"); + } + + int main() { + atexit(bye); + + std::string testPath = "/Script/WA-KA.txt"; + std::fstream str(testPath.c_str(), std::ios::in | std::ios::binary); + + if (str.is_open()) { - EMSCRIPTEN_PROFILE_INIT(3); - EMSCRIPTEN_PROFILE_BEGIN(0); - usleep(10 * 1000); - EMSCRIPTEN_PROFILE_END(0); - EMSCRIPTEN_PROFILE_BEGIN(1); - usleep(50 * 1000); - EMSCRIPTEN_PROFILE_END(1); - EMSCRIPTEN_PROFILE_BEGIN(2); - usleep(250 * 1000); - EMSCRIPTEN_PROFILE_END(2); - return 0; + std::cout << "open!" << std::endl; + } else { + std::cout << "missing!" << std::endl; } + + return 1; + } ''' + self.do_run(src, 'missing!\nall ok\n') - post1 = ''' -def process(filename): - src = open(filename, 'a') - src.write(\'\'\' - Profiling.dump(); - \'\'\') - src.close() -''' + def test_corruption_3(self): + if Settings.ASM_JS: return self.skip('cannot use corruption checks in asm') + if Settings.USE_TYPED_ARRAYS != 2: return self.skip('needs ta2 for actual test') - self.do_run(src, '''Profiling data: -Block 0: ''', post_build=post1) + Settings.CORRUPTION_CHECK = 1 + + # realloc + src = r''' + #include <stdlib.h> + #include <stdio.h> + #include <assert.h> + + void bye() { + printf("all ok\n"); + } + + int main(int argc, char **argv) { + atexit(bye); + + char *buffer = (char*)malloc(100); + for (int i = 0; i < 100; i++) buffer[i] = (i*i)%256; + buffer = (char*)realloc(buffer, argc + 50); + for (int i = 0; i < argc + 50; i++) { + //printf("%d : %d : %d : %d\n", i, (int)(buffer + i), buffer[i], (char)((i*i)%256)); + assert(buffer[i] == (char)((i*i)%256)); + } + return 1; + } + ''' + self.do_run(src, 'all ok\n') ### Integration tests @@ -7676,6 +8112,8 @@ def process(filename): def test_debug(self): if '-g' not in Building.COMPILER_TEST_OPTS: Building.COMPILER_TEST_OPTS.append('-g') + if self.emcc_args is not None: + if '-O1' in self.emcc_args or '-O2' in self.emcc_args: return self.skip('optimizations remove LLVM debug info') src = ''' #include <stdio.h> @@ -8051,17 +8489,14 @@ class %s(T): Settings.ASSERTIONS = 1-embetter Settings.SAFE_HEAP = 1-(embetter and llvm_opts) Building.LLVM_OPTS = llvm_opts - Settings.PGO = 0 Settings.CHECK_OVERFLOWS = 1-(embetter or llvm_opts) Settings.CORRECT_OVERFLOWS = 1-(embetter and llvm_opts) Settings.CORRECT_SIGNS = 0 Settings.CORRECT_ROUNDINGS = 0 Settings.CORRECT_OVERFLOWS_LINES = CORRECT_SIGNS_LINES = CORRECT_ROUNDINGS_LINES = SAFE_HEAP_LINES = [] Settings.CHECK_SIGNS = 0 #1-(embetter or llvm_opts) - Settings.INIT_STACK = 0 Settings.RUNTIME_TYPE_INFO = 0 Settings.DISABLE_EXCEPTION_CATCHING = 0 - Settings.PROFILE = 0 Settings.INCLUDE_FULL_LIBRARY = 0 Settings.BUILD_AS_SHARED_LIB = 0 Settings.RUNTIME_LINKED_LIBS = [] @@ -8084,11 +8519,11 @@ TT = %s exec('o1 = make_run("o1", compiler=CLANG, emcc_args=["-O1", "-s", "SAFE_HEAP=1"])') # Make one run with -O2, but without closure (we enable closure in specific tests, otherwise on everything it is too slow) - exec('o2 = make_run("o2", compiler=CLANG, emcc_args=["-O2", "--closure", "0"])') + exec('o2 = make_run("o2", compiler=CLANG, emcc_args=["-O2"])') # asm.js - #exec('asm = make_run("asm", compiler=CLANG, emcc_args=["-O0", "--closure", "0", "-s", "ASM_JS=1"])') - exec('asm2 = make_run("asm2", compiler=CLANG, emcc_args=["-O2", "--closure", "0", "-s", "ASM_JS=1"])') + #exec('asm = make_run("asm", compiler=CLANG, emcc_args=["-O0", "-s", "ASM_JS=1"])') + exec('asm2 = make_run("asm2", compiler=CLANG, emcc_args=["-O2", "-s", "ASM_JS=1"])') # Make custom runs with various options for compiler, quantum, embetter, typed_arrays, llvm_opts in [ @@ -8114,11 +8549,12 @@ TT = %s # --version output = Popen([PYTHON, compiler, '--version'], stdout=PIPE, stderr=PIPE).communicate() - self.assertContained('''emcc (Emscripten GCC-like replacement) 2.0 -Copyright (C) 2012 the Emscripten authors. + output = output[0].replace('\r', '') + self.assertContained('''emcc (Emscripten GCC-like replacement)''', output) + self.assertContained('''Copyright (C) 2013 the Emscripten authors (see AUTHORS.txt) This is free and open source software under the MIT license. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -''', output[0].replace('\r', ''), output[1].replace('\r', '')) +''', output) # -v, without input files output = Popen([PYTHON, compiler, '-v'], stdout=PIPE, stderr=PIPE).communicate() @@ -8224,20 +8660,23 @@ Options that are modified or new in %s include: (['-o', 'something.js', '-O0'], 0, None, 0, 0), (['-o', 'something.js', '-O1'], 1, None, 0, 0), (['-o', 'something.js', '-O1', '--closure', '1'], 1, None, 1, 0), - (['-o', 'something.js', '-O2'], 2, None, 1, 1), + (['-o', 'something.js', '-O2'], 2, None, 0, 1), (['-o', 'something.js', '-O2', '--closure', '0'], 2, None, 0, 0), + (['-o', 'something.js', '-O2', '-g'], 2, None, 0, 0), + (['-o', 'something.js', '-Os'], 2, None, 0, 1), (['-o', 'something.js', '-O3'], 3, None, 1, 1), (['-o', 'something.js', '-O3', '--closure', '0'], 3, None, 0, 0), # and, test compiling to bitcode first (['-o', 'something.bc'], 0, [], 0, 0), (['-o', 'something.bc'], 0, ['-O0'], 0, 0), (['-o', 'something.bc'], 1, ['-O1'], 0, 0), - (['-o', 'something.bc'], 2, ['-O2'], 1, 0), + (['-o', 'something.bc'], 2, ['-O2'], 0, 0), (['-o', 'something.bc'], 3, ['-O3'], 1, 0), (['-O1', '-o', 'something.bc'], 0, [], 0, 0), # -Ox is ignored and warned about ]: - #print params, opt_level, bc_params, closure + print params, opt_level, bc_params, closure, has_malloc self.clear() + keep_debug = '-g' in params output = Popen([PYTHON, compiler, path_from_root('tests', 'hello_world_loop' + ('_malloc' if has_malloc else '') + '.cpp')] + params, stdout=PIPE, stderr=PIPE).communicate() assert len(output[0]) == 0, output[0] @@ -8261,21 +8700,22 @@ Options that are modified or new in %s include: else: # closure has not been run, we can do some additional checks. TODO: figure out how to do these even with closure assert 'Module._main = ' not in generated, 'closure compiler should not have been run' - # XXX find a way to test this: assert ('& 255' in generated or '&255' in generated) == (opt_level <= 2), 'corrections should be in opt <= 2' - assert ('(label)' in generated) == (opt_level <= 1), 'relooping should be in opt >= 2' - assert ('assert(STACKTOP < STACK_MAX' in generated) == (opt_level == 0), 'assertions should be in opt == 0' - assert 'var $i;' in generated or 'var $i_0' in generated or 'var $storemerge3;' in generated or 'var $storemerge4;' in generated or 'var $i_04;' in generated, 'micro opts should always be on' + if keep_debug: + assert ('(label)' in generated) == (opt_level <= 1), 'relooping should be in opt >= 2' + assert ('assert(STACKTOP < STACK_MAX' in generated) == (opt_level == 0), 'assertions should be in opt == 0' + assert 'var $i;' in generated or 'var $i_0' in generated or 'var $storemerge3;' in generated or 'var $storemerge4;' in generated or 'var $i_04;' in generated, 'micro opts should always be on' if opt_level >= 2: - assert re.search('HEAP8\[\$\w+ \+ \(+\$\w+ ', generated) or re.search('HEAP8\[HEAP32\[', generated), 'eliminator should create compound expressions, and fewer one-time vars' # also in -O1, but easier to test in -O2 + assert re.search('HEAP8\[\$?\w+ \+ \(+\$?\w+ ', generated) or re.search('HEAP8\[HEAP32\[', generated), 'eliminator should create compound expressions, and fewer one-time vars' # also in -O1, but easier to test in -O2 assert ('_puts(' in generated) == (opt_level >= 1), 'with opt >= 1, llvm opts are run and they should optimize printf to puts' - assert ('function _malloc(bytes) {' in generated) == (not has_malloc), 'If malloc is needed, it should be there, if not not' assert 'function _main() {' in generated, 'Should be unminified, including whitespace' - assert ('-O3' in (params+(bc_params or []))) or'function _dump' in generated, 'No inlining by default' # emcc -s RELOOP=1 src.cpp ==> should pass -s to emscripten.py. --typed-arrays is a convenient alias for -s USE_TYPED_ARRAYS for params, test, text in [ + (['-s', 'ASM_JS=1', '-O2'], lambda generated: 'var i1 = 0' in generated, 'registerize is run by default in -O2'), + (['-s', 'ASM_JS=1', '-O2', '-g'], lambda generated: 'var i1 = 0' not in generated, 'registerize is cancelled by -g'), (['-s', 'INLINING_LIMIT=0'], lambda generated: 'function _dump' in generated, 'no inlining without opts'), - (['-O1', '-s', 'INLINING_LIMIT=0'], lambda generated: 'function _dump' not in generated, 'inlining'), + (['-O3', '-s', 'INLINING_LIMIT=0', '--closure', '0'], lambda generated: 'function _dump' not in generated, 'lto/inlining'), + (['-Os', '--llvm-lto', '1'], lambda generated: 'function _dump' in generated, '-Os disables inlining'), (['-s', 'USE_TYPED_ARRAYS=0'], lambda generated: 'new Int32Array' not in generated, 'disable typed arrays'), (['-s', 'USE_TYPED_ARRAYS=1'], lambda generated: 'IHEAPU = ' in generated, 'typed arrays 1 selected'), ([], lambda generated: 'Module["_dump"]' not in generated, 'dump is not exported by default'), @@ -8405,7 +8845,7 @@ f.close() # Run through node, if CMake produced a .js file. if cmake_outputs[i].endswith('.js'): - ret = Popen([NODE_JS, tempdirname + '/' + cmake_outputs[i]], stdout=PIPE).communicate()[0] + ret = Popen(listify(NODE_JS) + [tempdirname + '/' + cmake_outputs[i]], stdout=PIPE).communicate()[0] assert 'hello, world!' in ret, 'Running cmake-based .js application failed!' finally: os.chdir(path_from_root('tests')) # Move away from the directory we are about to remove. @@ -8431,14 +8871,6 @@ f.close() process.communicate() assert process.returncode is 0, 'User should be able to specify custom -std= on the command line!' - def test_Os(self): - for opt in ['s', '0']: - output = Popen([PYTHON, EMCC, path_from_root('tests', 'hello_world.c'), '-O' + opt], stdout=PIPE, stderr=PIPE).communicate() - assert len(output[0]) == 0, output[0] - assert ('emcc: warning: -Os is ignored (use -O0, -O1, -O2)' in output[1]) == (opt == 's'), 'warn on -Os when necessary' - assert os.path.exists('a.out.js'), '\n'.join(output) - self.assertContained('hello, world!', run_js('a.out.js')) - def test_catch_undef(self): open(os.path.join(self.get_dir(), 'test.cpp'), 'w').write(r''' #include <vector> @@ -8588,6 +9020,21 @@ f.close() self.assertContained('result: 62', run_js(os.path.join(self.get_dir(), 'a.out.js'))) + def test_asm_undefined(self): + src = r''' + #include <stdio.h> + extern void doit(); + int main(int argc, char **argv) { + if (argc == 121) doit(); + printf("done\n"); + return 1; + } + ''' + filename = self.in_dir('src.cpp') + open(filename, 'w').write(src) + out, err = Popen([PYTHON, EMCC, filename, '-s', 'ASM_JS=1', '-O2'], stderr=PIPE).communicate() + assert 'Warning: Unresolved symbol' in err, 'always warn on undefs in asm, since it breaks validation' + def test_redundant_link(self): lib = "int mult() { return 1; }" lib_name = os.path.join(self.get_dir(), 'libA.c') @@ -9011,7 +9458,7 @@ f.close() return 0; } ''') - Popen([PYTHON, EMCC, '-O2', '--closure', '-0', os.path.join(self.get_dir(), 'main.cpp')]).communicate() + Popen([PYTHON, EMCC, '-O2', os.path.join(self.get_dir(), 'main.cpp')]).communicate() output = run_js(os.path.join(self.get_dir(), 'a.out.js'), full_output=True, stderr=PIPE) self.assertContained('''0:0 1:1 @@ -9197,7 +9644,7 @@ f.close() (path_from_root('tools', 'test-js-optimizer-asm-last.js'), open(path_from_root('tools', 'test-js-optimizer-asm-last-output.js')).read(), ['asm', 'last']), ]: - output = Popen([NODE_JS, path_from_root('tools', 'js-optimizer.js'), input] + passes, stdin=PIPE, stdout=PIPE).communicate()[0] + output = Popen(listify(NODE_JS) + [path_from_root('tools', 'js-optimizer.js'), input] + passes, stdin=PIPE, stdout=PIPE).communicate()[0] self.assertIdentical(expected, output.replace('\r\n', '\n').replace('\n\n', '\n')) def test_m_mm(self): @@ -9213,8 +9660,8 @@ f.close() try: os.environ['EMCC_DEBUG'] = '1' for asm, linkable, chunks, js_chunks in [ - (0, 0, 3, 2), (0, 1, 7, 4), - (1, 0, 3, 2), (1, 1, 7, 5) + (0, 0, 3, 2), (0, 1, 4, 4), + (1, 0, 3, 2), (1, 1, 4, 5) ]: print asm, linkable, chunks, js_chunks output, err = Popen([PYTHON, EMCC, path_from_root('tests', 'hello_libcxx.cpp'), '-O1', '-s', 'LINKABLE=%d' % linkable, '-s', 'ASM_JS=%d' % asm], stdout=PIPE, stderr=PIPE).communicate() @@ -9223,6 +9670,26 @@ f.close() finally: del os.environ['EMCC_DEBUG'] + def test_debuginfo(self): + if os.environ.get('EMCC_DEBUG'): return self.skip('cannot run in debug mode') + try: + os.environ['EMCC_DEBUG'] = '1' + # llvm debug info is kept only when we can see it, which is without the js optimize, -O0. js debug info is lost by registerize in -O2, so - g disables it + for args, expect_llvm, expect_js in [ + (['-O0'], True, True), + (['-O0', '-g'], True, True), + (['-O1'], False, True), + (['-O1', '-g'], False, True), + (['-O2'], False, False), + (['-O2', '-g'], False, True), + ]: + print args, expect_llvm, expect_js + output, err = Popen([PYTHON, EMCC, path_from_root('tests', 'hello_world.cpp')] + args, stdout=PIPE, stderr=PIPE).communicate() + assert expect_llvm == ('strip-debug' not in err) + assert expect_js == ('registerize' not in err) + finally: + del os.environ['EMCC_DEBUG'] + def test_scons(self): # also incidentally tests c++11 integration in llvm 3.1 try_delete(os.path.join(self.get_dir(), 'test')) shutil.copytree(path_from_root('tests', 'scons'), os.path.join(self.get_dir(), 'test')) @@ -9373,6 +9840,81 @@ seeked= file. code = open('a.out.js').read() assert 'SAFE_HEAP' in code, 'valid -s option had an effect' + def test_optimize_normally(self): + assert not os.environ.get('EMCC_OPTIMIZE_NORMALLY') + assert not os.environ.get('EMCC_DEBUG') + + for optimize_normally in [0, 1]: + print optimize_normally + try: + if optimize_normally: os.environ['EMCC_OPTIMIZE_NORMALLY'] = '1' + os.environ['EMCC_DEBUG'] = '1' + + open(self.in_dir('main.cpp'), 'w').write(r''' + extern "C" { + void something(); + } + + int main() { + something(); + return 0; + } + ''') + open(self.in_dir('supp.cpp'), 'w').write(r''' + #include <stdio.h> + + extern "C" { + void something() { + printf("yello\n"); + } + } + ''') + out, err = Popen([PYTHON, EMCC, self.in_dir('main.cpp'), '-O2', '-o', 'main.o'], stdout=PIPE, stderr=PIPE).communicate() + assert ("emcc: LLVM opts: ['-O3']" in err) == optimize_normally + assert (' with -O3 since EMCC_OPTIMIZE_NORMALLY defined' in err) == optimize_normally + + out, err = Popen([PYTHON, EMCC, self.in_dir('supp.cpp'), '-O2', '-o', 'supp.o'], stdout=PIPE, stderr=PIPE).communicate() + assert ("emcc: LLVM opts: ['-O3']" in err) == optimize_normally + assert (' with -O3 since EMCC_OPTIMIZE_NORMALLY defined' in err) == optimize_normally + + out, err = Popen([PYTHON, EMCC, self.in_dir('main.o'), self.in_dir('supp.o'), '-O2', '-o', 'both.o'], stdout=PIPE, stderr=PIPE).communicate() + assert "emcc: LLVM opts: ['-O3']" not in err + assert ' with -O3 since EMCC_OPTIMIZE_NORMALLY defined' not in err + assert ('despite EMCC_OPTIMIZE_NORMALLY since not source code' in err) == optimize_normally + + out, err = Popen([PYTHON, EMCC, self.in_dir('main.cpp'), self.in_dir('supp.cpp'), '-O2', '-o', 'both2.o'], stdout=PIPE, stderr=PIPE).communicate() + assert ("emcc: LLVM opts: ['-O3']" in err) == optimize_normally + assert (' with -O3 since EMCC_OPTIMIZE_NORMALLY defined' in err) == optimize_normally + + for last in ['both.o', 'both2.o']: + out, err = Popen([PYTHON, EMCC, self.in_dir('both.o'), '-O2', '-o', last + '.js'], stdout=PIPE, stderr=PIPE).communicate() + assert ("emcc: LLVM opts: ['-O3']" not in err) == optimize_normally + assert ' with -O3 since EMCC_OPTIMIZE_NORMALLY defined' not in err + output = run_js(last + '.js') + assert 'yello' in output, 'code works' + assert open('both.o.js').read() == open('both2.o.js').read() + + finally: + if optimize_normally: del os.environ['EMCC_OPTIMIZE_NORMALLY'] + del os.environ['EMCC_DEBUG'] + + def test_jcache_printf(self): + open(self.in_dir('src.cpp'), 'w').write(r''' + #include <stdio.h> + #include <stdint.h> + #include <emscripten.h> + int main() { + emscripten_jcache_printf("hello world\n"); + emscripten_jcache_printf("hello %d world\n", 5); + emscripten_jcache_printf("hello %.3f world\n", 123.456789123); + emscripten_jcache_printf("hello %llx world\n", 0x1234567811223344ULL); + return 0; + } + ''') + Popen([PYTHON, EMCC, self.in_dir('src.cpp')]).communicate() + output = run_js('a.out.js') + self.assertIdentical('hello world\nhello 5 world\nhello 123.457 world\nhello 1234567811223300 world\n', output) + def test_conftest_s_flag_passing(self): open(os.path.join(self.get_dir(), 'conftest.c'), 'w').write(r''' int main() { @@ -9958,6 +10500,12 @@ elif 'browser' in str(sys.argv): Popen([PYTHON, EMCC, os.path.join(self.get_dir(), 'sdl_image.c'), '-O2', '--preload-file', 'screenshot.jpg', '-o', 'page.html']).communicate() self.run_browser('page.html', '', '/report_result?600') + def test_sdl_image_jpeg(self): + shutil.copyfile(path_from_root('tests', 'screenshot.jpg'), os.path.join(self.get_dir(), 'screenshot.jpeg')) + open(os.path.join(self.get_dir(), 'sdl_image_jpeg.c'), 'w').write(self.with_report_result(open(path_from_root('tests', 'sdl_image_jpeg.c')).read())) + Popen([PYTHON, EMCC, os.path.join(self.get_dir(), 'sdl_image_jpeg.c'), '--preload-file', 'screenshot.jpeg', '-o', 'page.html']).communicate() + self.run_browser('page.html', '', '/report_result?600') + def test_sdl_image_compressed(self): for image, width in [(path_from_root('tests', 'screenshot2.png'), 300), (path_from_root('tests', 'screenshot.jpg'), 600)]: @@ -9977,12 +10525,12 @@ elif 'browser' in str(sys.argv): self.run_browser('page.html', '', '/report_result?' + str(width)) def test_sdl_image_prepare(self): - # load an image file, get pixel data. Also O2 coverage for --preload-file + # load an image file, get pixel data. shutil.copyfile(path_from_root('tests', 'screenshot.jpg'), os.path.join(self.get_dir(), 'screenshot.not')) self.btest('sdl_image_prepare.c', reference='screenshot.jpg', args=['--preload-file', 'screenshot.not']) def test_sdl_image_prepare_data(self): - # load an image file, get pixel data. Also O2 coverage for --preload-file + # load an image file, get pixel data. shutil.copyfile(path_from_root('tests', 'screenshot.jpg'), os.path.join(self.get_dir(), 'screenshot.not')) self.btest('sdl_image_prepare_data.c', reference='screenshot.jpg', args=['--preload-file', 'screenshot.not']) @@ -10020,6 +10568,28 @@ elif 'browser' in str(sys.argv): Popen([PYTHON, EMCC, os.path.join(self.get_dir(), 'sdl_key.c'), '-o', 'page.html', '--pre-js', 'pre.js', '-s', '''EXPORTED_FUNCTIONS=['_main', '_one']''']).communicate() self.run_browser('page.html', '', '/report_result?510510') + def test_sdl_text(self): + open(os.path.join(self.get_dir(), 'pre.js'), 'w').write(''' + Module.postRun = function() { + function doOne() { + Module._one(); + setTimeout(doOne, 1000/60); + } + setTimeout(doOne, 1000/60); + } + + function simulateKeyEvent(charCode) { + var event = document.createEvent("KeyboardEvent"); + event.initKeyEvent("keypress", true, true, window, + 0, 0, 0, 0, 0, charCode); + document.body.dispatchEvent(event); + } + ''') + open(os.path.join(self.get_dir(), 'sdl_text.c'), 'w').write(self.with_report_result(open(path_from_root('tests', 'sdl_text.c')).read())) + + Popen([PYTHON, EMCC, os.path.join(self.get_dir(), 'sdl_text.c'), '-o', 'page.html', '--pre-js', 'pre.js', '-s', '''EXPORTED_FUNCTIONS=['_main', '_one']''']).communicate() + self.run_browser('page.html', '', '/report_result?1') + def test_sdl_mouse(self): open(os.path.join(self.get_dir(), 'pre.js'), 'w').write(''' function simulateMouseEvent(x, y, button) { @@ -10080,56 +10650,56 @@ elif 'browser' in str(sys.argv): # SDL, OpenGL, textures, immediate mode. Closure for more coverage shutil.copyfile(path_from_root('tests', 'screenshot.png'), os.path.join(self.get_dir(), 'screenshot.png')) self.reftest(path_from_root('tests', 'screenshot-gray-purple.png')) - Popen([PYTHON, EMCC, path_from_root('tests', 'sdl_ogl.c'), '-O2', '--minify', '0', '-o', 'something.html', '--pre-js', 'reftest.js', '--preload-file', 'screenshot.png']).communicate() + Popen([PYTHON, EMCC, path_from_root('tests', 'sdl_ogl.c'), '-O2', '--minify', '0', '-o', 'something.html', '--pre-js', 'reftest.js', '--preload-file', 'screenshot.png', '-s', 'GL_TESTING=1']).communicate() self.run_browser('something.html', 'You should see an image with gray at the top.', '/report_result?0') def test_sdl_ogl_defaultmatrixmode(self): # SDL, OpenGL, textures, immediate mode. Closure for more coverage shutil.copyfile(path_from_root('tests', 'screenshot.png'), os.path.join(self.get_dir(), 'screenshot.png')) self.reftest(path_from_root('tests', 'screenshot-gray-purple.png')) - Popen([PYTHON, EMCC, path_from_root('tests', 'sdl_ogl_defaultMatrixMode.c'), '--minify', '0', '-o', 'something.html', '--pre-js', 'reftest.js', '--preload-file', 'screenshot.png']).communicate() + Popen([PYTHON, EMCC, path_from_root('tests', 'sdl_ogl_defaultMatrixMode.c'), '--minify', '0', '-o', 'something.html', '--pre-js', 'reftest.js', '--preload-file', 'screenshot.png', '-s', 'GL_TESTING=1']).communicate() self.run_browser('something.html', 'You should see an image with gray at the top.', '/report_result?0') def test_sdl_ogl_p(self): # Immediate mode with pointers shutil.copyfile(path_from_root('tests', 'screenshot.png'), os.path.join(self.get_dir(), 'screenshot.png')) self.reftest(path_from_root('tests', 'screenshot-gray.png')) - Popen([PYTHON, EMCC, path_from_root('tests', 'sdl_ogl_p.c'), '-o', 'something.html', '--pre-js', 'reftest.js', '--preload-file', 'screenshot.png']).communicate() + Popen([PYTHON, EMCC, path_from_root('tests', 'sdl_ogl_p.c'), '-o', 'something.html', '--pre-js', 'reftest.js', '--preload-file', 'screenshot.png', '-s', 'GL_TESTING=1']).communicate() self.run_browser('something.html', 'You should see an image with gray at the top.', '/report_result?0') def test_sdl_fog_simple(self): # SDL, OpenGL, textures, fog, immediate mode. Closure for more coverage shutil.copyfile(path_from_root('tests', 'screenshot.png'), os.path.join(self.get_dir(), 'screenshot.png')) self.reftest(path_from_root('tests', 'screenshot-fog-simple.png')) - Popen([PYTHON, EMCC, path_from_root('tests', 'sdl_fog_simple.c'), '-O2', '--minify', '0', '-o', 'something.html', '--pre-js', 'reftest.js', '--preload-file', 'screenshot.png']).communicate() + Popen([PYTHON, EMCC, path_from_root('tests', 'sdl_fog_simple.c'), '-O2', '--minify', '0', '-o', 'something.html', '--pre-js', 'reftest.js', '--preload-file', 'screenshot.png', '-s', 'GL_TESTING=1']).communicate() self.run_browser('something.html', 'You should see an image with fog.', '/report_result?0') def test_sdl_fog_negative(self): # SDL, OpenGL, textures, fog, immediate mode. Closure for more coverage shutil.copyfile(path_from_root('tests', 'screenshot.png'), os.path.join(self.get_dir(), 'screenshot.png')) self.reftest(path_from_root('tests', 'screenshot-fog-negative.png')) - Popen([PYTHON, EMCC, path_from_root('tests', 'sdl_fog_negative.c'), '-o', 'something.html', '--pre-js', 'reftest.js', '--preload-file', 'screenshot.png']).communicate() + Popen([PYTHON, EMCC, path_from_root('tests', 'sdl_fog_negative.c'), '-o', 'something.html', '--pre-js', 'reftest.js', '--preload-file', 'screenshot.png', '-s', 'GL_TESTING=1']).communicate() self.run_browser('something.html', 'You should see an image with fog.', '/report_result?0') def test_sdl_fog_density(self): # SDL, OpenGL, textures, fog, immediate mode. Closure for more coverage shutil.copyfile(path_from_root('tests', 'screenshot.png'), os.path.join(self.get_dir(), 'screenshot.png')) self.reftest(path_from_root('tests', 'screenshot-fog-density.png')) - Popen([PYTHON, EMCC, path_from_root('tests', 'sdl_fog_density.c'), '-o', 'something.html', '--pre-js', 'reftest.js', '--preload-file', 'screenshot.png']).communicate() + Popen([PYTHON, EMCC, path_from_root('tests', 'sdl_fog_density.c'), '-o', 'something.html', '--pre-js', 'reftest.js', '--preload-file', 'screenshot.png', '-s', 'GL_TESTING=1']).communicate() self.run_browser('something.html', 'You should see an image with fog.', '/report_result?0') def test_sdl_fog_exp2(self): # SDL, OpenGL, textures, fog, immediate mode. Closure for more coverage shutil.copyfile(path_from_root('tests', 'screenshot.png'), os.path.join(self.get_dir(), 'screenshot.png')) self.reftest(path_from_root('tests', 'screenshot-fog-exp2.png')) - Popen([PYTHON, EMCC, path_from_root('tests', 'sdl_fog_exp2.c'), '-o', 'something.html', '--pre-js', 'reftest.js', '--preload-file', 'screenshot.png']).communicate() + Popen([PYTHON, EMCC, path_from_root('tests', 'sdl_fog_exp2.c'), '-o', 'something.html', '--pre-js', 'reftest.js', '--preload-file', 'screenshot.png', '-s', 'GL_TESTING=1']).communicate() self.run_browser('something.html', 'You should see an image with fog.', '/report_result?0') def test_sdl_fog_linear(self): # SDL, OpenGL, textures, fog, immediate mode. Closure for more coverage shutil.copyfile(path_from_root('tests', 'screenshot.png'), os.path.join(self.get_dir(), 'screenshot.png')) self.reftest(path_from_root('tests', 'screenshot-fog-linear.png')) - Popen([PYTHON, EMCC, path_from_root('tests', 'sdl_fog_linear.c'), '-o', 'something.html', '--pre-js', 'reftest.js', '--preload-file', 'screenshot.png']).communicate() + Popen([PYTHON, EMCC, path_from_root('tests', 'sdl_fog_linear.c'), '-o', 'something.html', '--pre-js', 'reftest.js', '--preload-file', 'screenshot.png', '-s', 'GL_TESTING=1']).communicate() self.run_browser('something.html', 'You should see an image with fog.', '/report_result?0') def test_worker(self): @@ -10228,7 +10798,6 @@ elif 'browser' in str(sys.argv): def chunked_server(support_byte_ranges): class ChunkedServerHandler(BaseHTTPServer.BaseHTTPRequestHandler): - @staticmethod def sendheaders(s, extra=[], length=len(data)): s.send_response(200) s.send_header("Content-Length", str(length)) @@ -10242,11 +10811,14 @@ elif 'browser' in str(sys.argv): s.end_headers() def do_HEAD(s): - ChunkedServerHandler.sendheaders(s) - + s.sendheaders() + + def do_OPTIONS(s): + s.sendheaders([("Access-Control-Allow-Headers", "Range")], 0) + def do_GET(s): if not support_byte_ranges: - ChunkedServerHandler.sendheaders(s) + s.sendheaders() s.wfile.write(data) else: (start, end) = s.headers.get("range").split("=")[1].split("-") @@ -10254,7 +10826,7 @@ elif 'browser' in str(sys.argv): end = int(end) end = min(len(data)-1, end) length = end-start+1 - ChunkedServerHandler.sendheaders(s,[],length) + s.sendheaders([],length) s.wfile.write(data[start:end+1]) s.wfile.close() httpd = BaseHTTPServer.HTTPServer(('localhost', 11111), ChunkedServerHandler) @@ -10269,26 +10841,34 @@ elif 'browser' in str(sys.argv): def test_glgears(self): self.reftest(path_from_root('tests', 'gears.png')) Popen([PYTHON, EMCC, path_from_root('tests', 'hello_world_gles.c'), '-o', 'something.html', - '-DHAVE_BUILTIN_SINCOS', '--pre-js', 'reftest.js']).communicate() + '-DHAVE_BUILTIN_SINCOS', '--pre-js', 'reftest.js', '-s', 'GL_TESTING=1']).communicate() self.run_browser('something.html', 'You should see animating gears.', '/report_result?0') def test_glgears_animation(self): - Popen([PYTHON, EMCC, path_from_root('tests', 'hello_world_gles.c'), '-o', 'something.html', - '-DHAVE_BUILTIN_SINCOS', - '--shell-file', path_from_root('tests', 'hello_world_gles_shell.html')]).communicate() - self.run_browser('something.html', 'You should see animating gears.', '/report_gl_result?true') + for full_es2 in [0, 1]: + for emulation in [0, 1]: + if full_es2 and emulation: continue + print full_es2, emulation + Popen([PYTHON, EMCC, path_from_root('tests', 'hello_world_gles%s.c' % ('' if not full_es2 else '_full')), '-o', 'something.html', + '-DHAVE_BUILTIN_SINCOS', '-s', 'GL_TESTING=1', + '--shell-file', path_from_root('tests', 'hello_world_gles_shell.html')] + + (['-s', 'FORCE_GL_EMULATION=1'] if emulation else []) + + (['-s', 'FULL_ES2=1'] if full_es2 else []), + ).communicate() + self.run_browser('something.html', 'You should see animating gears.', '/report_gl_result?true') + assert ('var GLEmulation' in open(self.in_dir('something.html')).read()) == emulation, "emulation code should be added when asked for" def test_glgears_bad(self): # Make sure that OpenGL ES is not available if typed arrays are not used Popen([PYTHON, EMCC, path_from_root('tests', 'hello_world_gles.c'), '-o', 'something.html', - '-DHAVE_BUILTIN_SINCOS', + '-DHAVE_BUILTIN_SINCOS', '-s', 'GL_TESTING=1', '-s', 'USE_TYPED_ARRAYS=0', '--shell-file', path_from_root('tests', 'hello_world_gles_shell.html')]).communicate() self.run_browser('something.html', 'You should not see animating gears.', '/report_gl_result?false') def test_glgears_deriv(self): self.reftest(path_from_root('tests', 'gears.png')) - Popen([PYTHON, EMCC, path_from_root('tests', 'hello_world_gles_deriv.c'), '-o', 'something.html', + Popen([PYTHON, EMCC, path_from_root('tests', 'hello_world_gles_deriv.c'), '-o', 'something.html', '-s', 'GL_TESTING=1', '-DHAVE_BUILTIN_SINCOS', '--pre-js', 'reftest.js']).communicate() self.run_browser('something.html', 'You should see animating gears.', '/report_result?0') src = open('something.html').read() @@ -10319,7 +10899,7 @@ elif 'browser' in str(sys.argv): args = ['--preload-file', 'smoke.tga', '-O2'] # test optimizations and closure here as well for more coverage self.reftest(book_path(basename.replace('.bc', '.png'))) - Popen([PYTHON, EMCC, program, '-o', 'program.html', '--pre-js', 'reftest.js'] + args).communicate() + Popen([PYTHON, EMCC, program, '-o', 'program.html', '--pre-js', 'reftest.js', '-s', 'GL_TESTING=1'] + args).communicate() self.run_browser('program.html', '', '/report_result?0') def btest(self, filename, expected=None, reference=None, reference_slack=0, args=[]): # TODO: use in all other tests @@ -10332,13 +10912,38 @@ elif 'browser' in str(sys.argv): open(os.path.join(self.get_dir(), filename), 'w').write(self.with_report_result(src)) else: expected = [str(i) for i in range(0, reference_slack+1)] - shutil.copyfile(path_from_root('tests', filename), os.path.join(self.get_dir(), filename)) + shutil.copyfile(path_from_root('tests', filename), os.path.join(self.get_dir(), os.path.basename(filename))) self.reftest(path_from_root('tests', reference)) - args = args + ['--pre-js', 'reftest.js'] - Popen([PYTHON, EMCC, os.path.join(self.get_dir(), filename), '-o', 'test.html'] + args).communicate() + args = args + ['--pre-js', 'reftest.js', '-s', 'GL_TESTING=1'] + Popen([PYTHON, EMCC, os.path.join(self.get_dir(), os.path.basename(filename)), '-o', 'test.html'] + args).communicate() if type(expected) is str: expected = [expected] self.run_browser('test.html', '.', ['/report_result?' + e for e in expected]) + def test_gles2_emulation(self): + shutil.copyfile(path_from_root('tests', 'glbook', 'Chapter_10', 'MultiTexture', 'basemap.tga'), self.in_dir('basemap.tga')) + shutil.copyfile(path_from_root('tests', 'glbook', 'Chapter_10', 'MultiTexture', 'lightmap.tga'), self.in_dir('lightmap.tga')) + shutil.copyfile(path_from_root('tests', 'glbook', 'Chapter_13', 'ParticleSystem', 'smoke.tga'), self.in_dir('smoke.tga')) + + for source, reference in [ + (os.path.join('glbook', 'Chapter_2', 'Hello_Triangle', 'Hello_Triangle_orig.c'), path_from_root('tests', 'glbook', 'CH02_HelloTriangle.png')), + #(os.path.join('glbook', 'Chapter_8', 'Simple_VertexShader', 'Simple_VertexShader_orig.c'), path_from_root('tests', 'glbook', 'CH08_SimpleVertexShader.png')), # XXX needs INT extension in WebGL + (os.path.join('glbook', 'Chapter_9', 'TextureWrap', 'TextureWrap_orig.c'), path_from_root('tests', 'glbook', 'CH09_TextureWrap.png')), + #(os.path.join('glbook', 'Chapter_9', 'Simple_TextureCubemap', 'Simple_TextureCubemap_orig.c'), path_from_root('tests', 'glbook', 'CH09_TextureCubemap.png')), # XXX needs INT extension in WebGL + (os.path.join('glbook', 'Chapter_9', 'Simple_Texture2D', 'Simple_Texture2D_orig.c'), path_from_root('tests', 'glbook', 'CH09_SimpleTexture2D.png')), + (os.path.join('glbook', 'Chapter_10', 'MultiTexture', 'MultiTexture_orig.c'), path_from_root('tests', 'glbook', 'CH10_MultiTexture.png')), + (os.path.join('glbook', 'Chapter_13', 'ParticleSystem', 'ParticleSystem_orig.c'), path_from_root('tests', 'glbook', 'CH13_ParticleSystem.png')), + ]: + print source + self.btest(source, + reference=reference, + args=['-I' + path_from_root('tests', 'glbook', 'Common'), + path_from_root('tests', 'glbook', 'Common', 'esUtil.c'), + path_from_root('tests', 'glbook', 'Common', 'esShader.c'), + path_from_root('tests', 'glbook', 'Common', 'esShapes.c'), + path_from_root('tests', 'glbook', 'Common', 'esTransform.c'), + '-s', 'FULL_ES2=1', + '--preload-file', 'basemap.tga', '--preload-file', 'lightmap.tga', '--preload-file', 'smoke.tga']) + def test_emscripten_api(self): self.btest('emscripten_api_browser.cpp', '1', args=['-s', '''EXPORTED_FUNCTIONS=['_main', '_third']''']) @@ -10369,6 +10974,19 @@ elif 'browser' in str(sys.argv): shutil.copyfile(path_from_root('tests', 'screenshot.png'), os.path.join(self.get_dir(), 'screenshot.png')) self.btest('gl_ps.c', reference='gl_ps.png', args=['--preload-file', 'screenshot.png']) + def test_gl_ps_packed(self): + # packed data that needs to be strided + shutil.copyfile(path_from_root('tests', 'screenshot.png'), os.path.join(self.get_dir(), 'screenshot.png')) + self.btest('gl_ps_packed.c', reference='gl_ps.png', args=['--preload-file', 'screenshot.png']) + + def test_gl_ps_workaround(self): + shutil.copyfile(path_from_root('tests', 'screenshot.png'), os.path.join(self.get_dir(), 'screenshot.png')) + self.btest('gl_ps_workaround.c', reference='gl_ps.png', args=['--preload-file', 'screenshot.png']) + + def test_gl_ps_workaround2(self): + shutil.copyfile(path_from_root('tests', 'screenshot.png'), os.path.join(self.get_dir(), 'screenshot.png')) + self.btest('gl_ps_workaround2.c', reference='gl_ps.png', args=['--preload-file', 'screenshot.png']) + def test_matrix_identity(self): self.btest('gl_matrix_identity.c', expected=['-1882984448', '460451840']) @@ -10417,6 +11035,15 @@ elif 'browser' in str(sys.argv): def test_cubegeom_fog(self): self.btest('cubegeom_fog.c', expected=['1617140399', '-898782526', '-946179526']) + def test_cubegeom_pre_vao(self): + self.btest('cubegeom_pre_vao.c', expected=['-1472804742', '-1626058463', '-2046234971']) + + def test_cubegeom_pre2_vao(self): + self.btest('cubegeom_pre2_vao.c', expected=['-1472804742', '-1626058463', '-2046234971']) + + def test_cubegeom_pre2_vao2(self): + self.btest('cubegeom_pre2_vao2.c', expected=['-790445118']) + def test_cube_explosion(self): self.btest('cube_explosion.c', expected=['667220544', '-1543354600', '-1485258415']) @@ -10499,14 +11126,17 @@ elif 'browser' in str(sys.argv): def test_subdata(self): self.btest('gl_subdata.cpp', reference='float_tex.png') + def test_perspective(self): + self.btest('perspective.c', reference='perspective.png') + def test_runtimelink(self): main, supp = self.setup_runtimelink_test() open(self.in_dir('supp.cpp'), 'w').write(supp) - Popen([PYTHON, EMCC, self.in_dir('supp.cpp'), '-o', 'supp.js', '-s', 'LINKABLE=1', '-s', 'NAMED_GLOBALS=1', '-s', 'BUILD_AS_SHARED_LIB=2', '-O2', '--closure', '0']).communicate() + Popen([PYTHON, EMCC, self.in_dir('supp.cpp'), '-o', 'supp.js', '-s', 'LINKABLE=1', '-s', 'NAMED_GLOBALS=1', '-s', 'BUILD_AS_SHARED_LIB=2', '-O2']).communicate() shutil.move(self.in_dir('supp.js'), self.in_dir('supp.so')) - self.btest(main, args=['-s', 'LINKABLE=1', '-s', 'NAMED_GLOBALS=1', '-s', 'RUNTIME_LINKED_LIBS=["supp.so"]', '-DBROWSER=1', '-O2', '--closure', '0'], expected='76') + self.btest(main, args=['-s', 'LINKABLE=1', '-s', 'NAMED_GLOBALS=1', '-s', 'RUNTIME_LINKED_LIBS=["supp.so"]', '-DBROWSER=1', '-O2'], expected='76') def test_pre_run_deps(self): # Adding a dependency in preRun will delay run @@ -10753,7 +11383,7 @@ elif 'benchmark' in str(sys.argv): Building.COMPILER_TEST_OPTS = [] TEST_REPS = 4 - TOTAL_TESTS = 9 + TOTAL_TESTS = 8 tests_done = 0 total_times = map(lambda x: 0., range(TOTAL_TESTS)) @@ -10802,12 +11432,15 @@ elif 'benchmark' in str(sys.argv): try_delete(final_filename) output = Popen([PYTHON, EMCC, filename, #'-O3', - '-O2', '-s', 'INLINING_LIMIT=0', '-s', 'DOUBLE_MODE=0', '-s', 'PRECISE_I64_MATH=0', - '-s', 'ASM_JS=1',# '-s', 'USE_MATH_IMUL=1', + '-O2', '-s', 'INLINING_LIMIT=0', '-s', 'DOUBLE_MODE=0', '-s', 'PRECISE_I64_MATH=0',# '-s', 'EXPLICIT_ZEXT=1', + '-s', 'ASM_JS=1', '-s', 'USE_MATH_IMUL=1', '--llvm-lto', '1', '-s', 'TOTAL_MEMORY=128*1024*1024', '-s', 'FAST_MEMORY=10*1024*1024', '-o', final_filename] + shared_args + emcc_args, stdout=PIPE, stderr=self.stderr_redirect).communicate() assert os.path.exists(final_filename), 'Failed to compile file: ' + output[0] + if self.save_JS: + self.hardcode_arguments(final_filename, args) + # Run JS global total_times, tests_done times = [] @@ -11057,11 +11690,6 @@ elif 'benchmark' in str(sys.argv): -------------------------------- ''', shared_args=['-std=c99'], force_c=True) - def test_dlmalloc(self): - # XXX This seems to have regressed slightly with emcc. Are -g and the signs lines passed properly? - src = open(path_from_root('system', 'lib', 'dlmalloc.c'), 'r').read() + '\n\n\n' + open(path_from_root('tests', 'dlmalloc_test.c'), 'r').read() - self.do_benchmark('dlmalloc', src, ['400', '3000'], '*3000,0*', emcc_args=['-g', '-s', 'CORRECT_SIGNS=2', '-s', 'CORRECT_SIGNS_LINES=[4820, 4195, 4250, 4203, 4209, 4239, 4231]']) - def test_zlib(self): src = open(path_from_root('tests', 'zlib', 'benchmark.c'), 'r').read() emcc_args = self.get_library('zlib', os.path.join('libz.a'), make_args=['libz.a']) + \ @@ -11220,12 +11848,12 @@ elif 'sanity' in str(sys.argv): f = open(CONFIG_FILE, 'a') f.write('CLOSURE_COMPILER = "/tmp/nowhere/nothingtoseehere/kjadsfkjwelkjsdfkqgas/nonexistent.txt"\n') f.close() - output = self.check_working([EMCC, '-O2', 'tests/hello_world.cpp'], CLOSURE_FATAL) + output = self.check_working([EMCC, '-O2', '--closure', '1', 'tests/hello_world.cpp'], CLOSURE_FATAL) # With a working path, all is well restore() try_delete('a.out.js') - output = self.check_working([EMCC, '-O2', 'tests/hello_world.cpp'], '') + output = self.check_working([EMCC, '-O2', '--closure', '1', 'tests/hello_world.cpp'], '') assert os.path.exists('a.out.js') def test_llvm(self): @@ -11427,7 +12055,7 @@ fi try_delete(basebc_name) # we might need to check this file later try_delete(dcebc_name) # we might need to check this file later for ll_name in ll_names: try_delete(ll_name) - output = self.do([EMCC, '-O' + str(i), '--closure', '0', '-s', 'RELOOP=0', '--llvm-lto', '0', path_from_root('tests', filename)]) + output = self.do([EMCC, '-O' + str(i), '-s', 'RELOOP=0', '--llvm-lto', '0', path_from_root('tests', filename)]) #print output assert INCLUDING_MESSAGE.replace('X', libname) in output if libname == 'libc': @@ -11466,6 +12094,8 @@ fi try_delete(CANONICAL_TEMP_DIR) def test_relooper(self): + RELOOPER = Cache.get_path('relooper.js') + restore() for phase in range(2): # 0: we wipe the relooper dir. 1: we have it, so should just update if phase == 0: Cache.erase() @@ -11475,7 +12105,7 @@ fi print >> sys.stderr, phase, i opt = min(i, 2) try_delete('a.out.js') - output = Popen([PYTHON, EMCC, path_from_root('tests', 'hello_world_loop.cpp'), '-O' + str(opt), '--closure', '0'], + output = Popen([PYTHON, EMCC, path_from_root('tests', 'hello_world_loop.cpp'), '-O' + str(opt)], stdout=PIPE, stderr=PIPE).communicate() self.assertContained('hello, world!', run_js('a.out.js')) output = '\n'.join(output) @@ -11499,6 +12129,8 @@ fi try: os.environ['EMCC_DEBUG'] = '1' + os.environ['EMCC_JSOPT_MIN_CHUNK_SIZE'] = str(1024*512) + self.working_dir = os.path.join(TEMP_DIR, 'emscripten_temp') if not os.path.exists(self.working_dir): os.makedirs(self.working_dir) @@ -11525,17 +12157,25 @@ fi (['--jcache'], 'hello_malloc.cpp', False, True, False, True, False, True, []), ([], 'hello_malloc.cpp', False, False, False, False, False, False, []), # new, huge file - ([], 'hello_libcxx.cpp', False, False, False, False, False, False, ('2 chunks', '3 chunks')), + ([], 'hello_libcxx.cpp', False, False, False, False, False, False, ('3 chunks',)), (['--jcache'], 'hello_libcxx.cpp', True, False, True, False, True, False, []), (['--jcache'], 'hello_libcxx.cpp', False, True, False, True, False, True, []), ([], 'hello_libcxx.cpp', False, False, False, False, False, False, []), # finally, build a file close to the previous, to see that some chunks are found in the cache and some not - (['--jcache'], 'hello_libcxx_mod1.cpp', False, True, True, True, True, False, []), # win on pre, mix on funcs, lose on jsfuncs + (['--jcache'], 'hello_libcxx_mod1.cpp', False, True, True, True, True, True, []), # win on pre, mix on funcs, mix on jsfuncs (['--jcache'], 'hello_libcxx_mod1.cpp', False, True, False, True, False, True, []), + (None, None, None, None, None, None, None, None, None), # clear + (['--jcache'], 'hello_libcxx_mod2.cpp', True, False, True, False, True, False, []), # load into cache + (['--jcache'], 'hello_libcxx_mod2a.cpp', False, True, True, True, True, True, []) # add a printf, do not lose everything ]: - print >> sys.stderr, args, input_file, expect_pre_save, expect_pre_load, expect_funcs_save, expect_funcs_load, expect_jsfuncs_save, expect_jsfuncs_load, expected self.clear() - out, err = Popen([PYTHON, EMCC, '-O2', '--closure', '0', path_from_root('tests', input_file)] + args, stdout=PIPE, stderr=PIPE).communicate() + if args is None: + Cache.erase() + continue + + print >> sys.stderr, args, input_file, expect_pre_save, expect_pre_load, expect_funcs_save, expect_funcs_load, expect_jsfuncs_save, expect_jsfuncs_load, expected + + out, err = Popen([PYTHON, EMCC, '-O2', path_from_root('tests', input_file)] + args, stdout=PIPE, stderr=PIPE).communicate() errtail = err.split('emcc invocation')[-1] self.assertContained('hello, world!', run_js('a.out.js'), errtail) assert (PRE_SAVE_MSG in err) == expect_pre_save, errtail @@ -11549,8 +12189,8 @@ fi if input_file not in srcs: srcs[input_file] = curr else: - open('/home/alon/Dev/emscripten/a', 'w').write(srcs[input_file]) - open('/home/alon/Dev/emscripten/b', 'w').write(curr) + #open('/home/alon/Dev/emscripten/a', 'w').write(srcs[input_file]) + #open('/home/alon/Dev/emscripten/b', 'w').write(curr) assert abs(len(curr)/float(len(srcs[input_file]))-1)<0.01, 'contents may shift in order, but must remain the same size %d vs %d' % (len(curr), len(srcs[input_file])) + '\n' + errtail used_jcache = used_jcache or ('--jcache' in args) assert used_jcache == os.path.exists(JCache.get_cachename('emscript_files')) @@ -11558,6 +12198,7 @@ fi finally: del os.environ['EMCC_DEBUG'] + del os.environ['EMCC_JSOPT_MIN_CHUNK_SIZE'] else: raise Exception('Test runner is confused: ' + str(sys.argv)) diff --git a/tests/s3tc_crunch.png b/tests/s3tc_crunch.png Binary files differindex 3510b83b..067be7aa 100644 --- a/tests/s3tc_crunch.png +++ b/tests/s3tc_crunch.png diff --git a/tests/screenshot-fog-density.png b/tests/screenshot-fog-density.png Binary files differindex cd1f6f1b..60f0c9ef 100644 --- a/tests/screenshot-fog-density.png +++ b/tests/screenshot-fog-density.png diff --git a/tests/screenshot-fog-exp2.png b/tests/screenshot-fog-exp2.png Binary files differindex cd5e6a63..4737a536 100644 --- a/tests/screenshot-fog-exp2.png +++ b/tests/screenshot-fog-exp2.png diff --git a/tests/screenshot-fog-linear.png b/tests/screenshot-fog-linear.png Binary files differindex 57534566..747c0c25 100644 --- a/tests/screenshot-fog-linear.png +++ b/tests/screenshot-fog-linear.png diff --git a/tests/screenshot-fog-negative.png b/tests/screenshot-fog-negative.png Binary files differindex 5b18a201..747063c9 100644 --- a/tests/screenshot-fog-negative.png +++ b/tests/screenshot-fog-negative.png diff --git a/tests/screenshot-fog-simple.png b/tests/screenshot-fog-simple.png Binary files differindex 527768fc..7624076c 100644 --- a/tests/screenshot-fog-simple.png +++ b/tests/screenshot-fog-simple.png diff --git a/tests/screenshot-gray-purple.png b/tests/screenshot-gray-purple.png Binary files differindex 514b29a1..eaf08e6a 100644 --- a/tests/screenshot-gray-purple.png +++ b/tests/screenshot-gray-purple.png diff --git a/tests/screenshot-gray.png b/tests/screenshot-gray.png Binary files differindex 16e45a7a..af06c256 100644 --- a/tests/screenshot-gray.png +++ b/tests/screenshot-gray.png diff --git a/tests/sdl_audio.c b/tests/sdl_audio.c index 938df3c4..ce3bf5a9 100644 --- a/tests/sdl_audio.c +++ b/tests/sdl_audio.c @@ -6,13 +6,14 @@ Mix_Chunk *sound, *sound2; -void play2(); +int play2(); -void play() { +int play() { int channel = Mix_PlayChannel(-1, sound, 1); assert(channel == 0); emscripten_run_script("setTimeout(Module['_play2'], 500)"); + return channel; } void done(int channel) { @@ -22,11 +23,12 @@ void done(int channel) { REPORT_RESULT(); } -void play2() { +int play2() { Mix_ChannelFinished(done); int channel2 = Mix_PlayChannel(-1, sound2, 1); assert(channel2 == 1); + return channel2; } int main(int argc, char **argv) { @@ -40,7 +42,17 @@ int main(int argc, char **argv) { sound2 = Mix_LoadWAV("sound2.wav"); assert(sound); - play(); + int channel = play(); + printf( "Pausing Channel %d", channel ); + Mix_Pause(channel); + int paused = Mix_Paused(channel); + printf( "Channel %d %s", channel, paused ? "is paused" : "is NOT paused" ); + assert(paused); + Mix_Resume(channel); + paused = Mix_Paused(channel); + printf( "Channel %d %s", channel, paused ? "is paused" : "is NOT paused" ); + assert(paused == 0); + if (argc == 12121) play2(); // keep it alive emscripten_run_script("element = document.createElement('input');" diff --git a/tests/sdl_gl_read.c b/tests/sdl_gl_read.c index 552eb8c0..d752f94d 100644 --- a/tests/sdl_gl_read.c +++ b/tests/sdl_gl_read.c @@ -6,6 +6,7 @@ #include <stdio.h> #include <stdlib.h> +#include <assert.h> GLuint programObject; int width = 512; @@ -118,8 +119,14 @@ void Draw () } void Verify() { - unsigned char *data = malloc(width*height*4); + unsigned char *data = malloc(width*height*4 + 16); + int *last = (int*)(data + width*height*4 - 4); + int *after = (int*)(data + width*height*4); + *last = 0xdeadbeef; + *after = 0x12345678; glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data); + assert(*last != 0xdeadbeef); // should overwrite the buffer to the end + assert(*after == 0x12345678); // nothing should be written afterwards! // Should see some blue, and nothing else int seen = 0; int ok = 1; diff --git a/tests/sdl_image_jpeg.c b/tests/sdl_image_jpeg.c new file mode 100644 index 00000000..10619dad --- /dev/null +++ b/tests/sdl_image_jpeg.c @@ -0,0 +1,45 @@ +#include <stdio.h> +#include <SDL/SDL.h> +#include <SDL/SDL_image.h> +#include <assert.h> +#include <emscripten.h> + +int testImage(SDL_Surface* screen, const char* fileName) { + SDL_Surface *image = IMG_Load(fileName); + if (!image) + { + printf("IMG_Load: %s\n", IMG_GetError()); + return 0; + } + assert(image->format->BitsPerPixel == 32); + assert(image->format->BytesPerPixel == 4); + assert(image->pitch == 4*image->w); + int result = image->w; + + SDL_BlitSurface (image, NULL, screen, NULL); + SDL_FreeSurface (image); + + return result; +} + +int main() { + SDL_Init(SDL_INIT_VIDEO); + SDL_Surface *screen = SDL_SetVideoMode(600, 450, 32, SDL_SWSURFACE); + + int result = 0; + result = testImage(screen, "screenshot.jpeg"); // relative path + assert(result != 0); + result |= testImage(screen, "/screenshot.jpeg"); // absolute path + assert(result != 0); + + SDL_Flip(screen); + + printf("you should see an image.\n"); + + SDL_Quit(); + + REPORT_RESULT(); + + return 0; +} + diff --git a/tests/sdl_text.c b/tests/sdl_text.c new file mode 100644 index 00000000..6bcb0d61 --- /dev/null +++ b/tests/sdl_text.c @@ -0,0 +1,40 @@ +#include <SDL/SDL.h> +#include <stdio.h> +#include <string.h> +#include <assert.h> +#include <emscripten.h> + +int result = 0; + +void one() { + SDL_Event event; + while (SDL_PollEvent(&event)) { + switch (event.type) { + case SDL_TEXTEDITING: assert(0); break; + case SDL_TEXTINPUT: + printf("Received %s\n", event.text.text); + if (!strcmp("a", event.text.text)) { + result = 1; + } else if (!strcmp("A", event.text.text)) { + REPORT_RESULT(); + emscripten_run_script("throw 'done'"); + } + break; + default: /* Report an unhandled event */ + printf("I don't know what this event is!\n"); + } + } +} + +int main() { + SDL_Init(SDL_INIT_VIDEO); + SDL_SetVideoMode(600, 450, 32, SDL_HWSURFACE); + SDL_StartTextInput(); + + emscripten_run_script("simulateKeyEvent('a'.charCodeAt(0))"); // a + emscripten_run_script("simulateKeyEvent('A'.charCodeAt(0))"); // A + + one(); + + return 0; +} diff --git a/tests/websockets.c b/tests/websockets.c index 59acbd69..8e719baa 100644 --- a/tests/websockets.c +++ b/tests/websockets.c @@ -27,14 +27,19 @@ unsigned int get_all_buf(int sock, char* output, unsigned int maxsize) assert(select(64, &sett, NULL, NULL, NULL) == 0); // empty set FD_SET(sock, &sett); assert(select(0, &sett, NULL, NULL, NULL) == 0); // max FD to check is 0 + assert(FD_ISSET(sock, &sett) == 0); + FD_SET(sock, &sett); int select_says_yes = select(64, &sett, NULL, NULL, NULL); // ioctl check for IO int bytes; if (ioctl(sock, FIONREAD, &bytes) || bytes == 0) { not_always_data = 1; + assert(FD_ISSET(sock, &sett) == 0); return 0; } + + assert(FD_ISSET(sock, &sett)); assert(select_says_yes); // ioctl must agree with select char buffer[1024]; @@ -82,7 +87,7 @@ void iter(void *arg) { printf("sum: %d\n", sum); #if EMSCRIPTEN - assert(not_always_data == 1); + //assert(not_always_data == 1); int result = sum; REPORT_RESULT(); diff --git a/third_party/jni/emjvm.cpp b/third_party/jni/emjvm.cpp new file mode 100644 index 00000000..77ec3871 --- /dev/null +++ b/third_party/jni/emjvm.cpp @@ -0,0 +1,133 @@ + +// Emscripten shims for JVM support + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <assert.h> + +#include "emjvm.h" + +// invoke interface + +jint DestroyJavaVM(JavaVM*) { exit(55); return NULL; } +jint AttachCurrentThread(JavaVM*, JNIEnv**, void*) { exit(66); return NULL; } +jint DetachCurrentThread(JavaVM*) { exit(77); return NULL; } +jint GetEnv(JavaVM*, void** env, jint); // forward def +jint AttachCurrentThreadAsDaemon(JavaVM*, JNIEnv**, void*) { exit(88); return NULL; } + +// env - some of these are externs that are implemented in JS + +jobject EMJVM_NewGlobalRef(JNIEnv*, jobject obj) { + return obj; // XXX no global refcounting, we just keep global singletons alive etc. +} + +extern "C" { + extern jstring emjvm_newString(const jchar *chars, jsize len); +} +jstring EMJVM_NewString(JNIEnv*, const jchar* unicodeChars, jsize len) { + return emjvm_newString(unicodeChars, len); +} + +extern "C" { + extern jclass emjvm_getObjectClass(JNIEnv*, jobject obj); +} + +extern "C" { + extern jmethodID emjvm_getMethodID(jclass, const char*, const char*); +} +extern jmethodID EMJVM_GetMethodID(JNIEnv*, jclass clazz, const char* methodName, const char* sig) { + return emjvm_getMethodID(clazz, methodName, sig); +} + +extern "C" { + extern jobject emjvm_callObjectMethod(JNIEnv*, jobject, jmethodID, va_list); + + extern void emjvm_deleteLocalRef(JNIEnv*, jobject); + + jsize emjvm_getArrayLength(JNIEnv*, jarray); + + void emjvm_getByteArrayRegion(JNIEnv*, jbyteArray, jsize, jsize, jbyte*); + + jclass emjvm_findClass(JNIEnv*, const char*); + + jmethodID emjvm_getStaticMethodID(JNIEnv*, jclass, const char*, const char*); + + jobject emjvm_callStaticObjectMethod(JNIEnv*, jclass, jmethodID, va_list); + + jboolean emjvm_callBooleanMethod(JNIEnv*, jobject, jmethodID, va_list); + jboolean emjvm_callStaticBooleanMethod(JNIEnv*, jclass, jmethodID, va_list); + + void emjvm_callVoidMethod(JNIEnv*, jobject, jmethodID, va_list); + + jint emjvm_callIntMethod(JNIEnv*, jobject, jmethodID, va_list); + + const char* emjvm_getStringUTFChars(JNIEnv*, jstring, jboolean*); + jsize emjvm_getStringUTFLength(JNIEnv*, jstring); + void emjvm_releaseStringUTFChars(JNIEnv*, jstring, const char*); +} + +// JVM + +struct EmJVM { + JavaVM jvm; + JNIInvokeInterface jvmFunctions; + + JNIEnv env; + JNINativeInterface envFunctions; + + EmJVM() { + // jvm + jvm.functions = &jvmFunctions; + + jvmFunctions.DestroyJavaVM = DestroyJavaVM; + jvmFunctions.AttachCurrentThread = AttachCurrentThread; + jvmFunctions.DetachCurrentThread = DetachCurrentThread; + jvmFunctions.GetEnv = GetEnv; + jvmFunctions.AttachCurrentThreadAsDaemon = AttachCurrentThreadAsDaemon; + + // env + memset(&envFunctions, 0, sizeof(envFunctions)); + + env.functions = &envFunctions; + + envFunctions.NewGlobalRef = EMJVM_NewGlobalRef; + envFunctions.NewString = EMJVM_NewString; + envFunctions.GetObjectClass = emjvm_getObjectClass; + envFunctions.GetMethodID = EMJVM_GetMethodID; + envFunctions.CallObjectMethodV = emjvm_callObjectMethod; + envFunctions.DeleteLocalRef = emjvm_deleteLocalRef; + envFunctions.GetArrayLength = emjvm_getArrayLength; + envFunctions.GetByteArrayRegion = emjvm_getByteArrayRegion; + envFunctions.FindClass = emjvm_findClass; + envFunctions.GetStaticMethodID = emjvm_getStaticMethodID; + envFunctions.CallStaticObjectMethodV = emjvm_callStaticObjectMethod; + envFunctions.CallBooleanMethodV = emjvm_callBooleanMethod; + envFunctions.CallStaticBooleanMethodV = emjvm_callStaticBooleanMethod; + envFunctions.CallVoidMethodV = emjvm_callVoidMethod; + envFunctions.CallIntMethodV = emjvm_callIntMethod; + envFunctions.GetStringUTFChars = emjvm_getStringUTFChars; + envFunctions.GetStringUTFLength = emjvm_getStringUTFLength; + envFunctions.ReleaseStringUTFChars = emjvm_releaseStringUTFChars; + } +}; + +EmJVM emJVM; + +// implement forward defs + +jint GetEnv(JavaVM*, void** env, jint) { + *env = &emJVM.env; + return 0; +} + +// external access from JS + +extern "C" { + +JavaVM* emscripten_get_jvm() { return &emJVM.jvm; } + +JNIEnv* emscripten_get_jni_env() { return &emJVM.env; } + +} + diff --git a/third_party/jni/emjvm.h b/third_party/jni/emjvm.h new file mode 100644 index 00000000..fb09413a --- /dev/null +++ b/third_party/jni/emjvm.h @@ -0,0 +1,8 @@ + +#include "jni.h" + +extern "C" { + extern JavaVM* emscripten_get_jvm(); + extern JNIEnv* emscripten_get_jni_env(); +} + diff --git a/third_party/jni/emjvm.js b/third_party/jni/emjvm.js new file mode 100644 index 00000000..e422d208 --- /dev/null +++ b/third_party/jni/emjvm.js @@ -0,0 +1,180 @@ + +var EmJVM = { + debug: false, + + nextId: 0, + objects: {}, + classNames: {}, // class name => singleton object + + addObject: function(o) { + var ret = EmJVM.nextId++; + EmJVM.objects[ret] = o; + o.id = ret; + o.refs = 1; + o.nextMethodId = 0; + // XXX Module.print('add object ' + JSON.stringify(o).substr(0, 80) + (ret > 5285 ? new Error().stack : '')); + return ret; + }, + + addSingletonObject: function(o) { + EmJVM.classNames[o.name] = o; + return EmJVM.addObject(o); + }, + + createString: function(data) { + return EmJVM.addObject({ name: 'string', value: data }); + }, + + createByteArray: function(data) { + return EmJVM.addObject({ name: 'byteArray', value: data }); + }, +}; + +function widecharToString(ptr, len) { + var nullTerminated = typeof(len) == "undefined"; + var ret = ""; + var i = 0; + var t; + while (1) { + t = getValue(ptr + 2 * i, 'i16'); + if (nullTerminated && t == 0) break; + if (t != 0) { + ret += String.fromCharCode(t); + } + ++i; + if (!nullTerminated && i == len) break; + }; + return ret; +} + +function _emjvm_newString(chars, len) { + return EmJVM.createString(widecharToString(chars, len)); +} + +function _emjvm_getStringUTFChars(jniEnv, string, isCopy) { + var obj = EmJVM.objects[string]; + assert(obj.name == 'string'); + if (isCopy) setValue(isCopy, 'i8', 1); + var buffer = _malloc(obj.value.length+1); + writeStringToMemory(obj.value, buffer); + return buffer; +} + +function _emjvm_getStringUTFLength(jniEnv, string) { + var obj = EmJVM.objects[string]; + if (obj.value) { + return obj.value.length; + } + return 0; +} + +function _emjvm_releaseStringUTFChars(jniEnv, string, utf) { +} + +function _emjvm_getObjectClass(env, jobject) { + if (EmJVM.debug) { + console.log('EMJVM_GetObjectClass+AddLocalRef: ' + [jobject]); + } + var obj = EmJVM.objects[jobject]; + obj.refs++; + return jobject; +} + +function _emjvm_getMethodID(jclass, name, sig) { + if (EmJVM.debug) { + console.log('EMJVM_GetMethodID: ' + [jclass, Pointer_stringify(name), Pointer_stringify(sig)]); + console.log('EMJVM_GetMethodID: ' + [EmJVM.objects[jclass].name]); + } + // assumes class <--> object, just called on singletons + name = Pointer_stringify(name); + var obj = EmJVM.objects[jclass]; + if (!obj[name]) { + throw 'missing implementation for ' + obj.name + '::' + name + ' : ' + new Error().stack; + } + if (!obj[name + '__methodId']) { + var methodId = obj.nextMethodId++; + obj[name + '__methodId'] = methodId; + obj['method__' + methodId] = obj[name]; + obj['methodName__' + methodId] = name; + } + return obj[name + '__methodId']; +} + +function _emjvm_getStaticMethodID(jniEnv, jclass, name, sig) { + // Pretend this to be the same as looking up a non-static method + return _emjvm_getMethodID(jclass, name, sig); +} + +function _emjvm_callObjectMethod(jniEnv, jobject, methodId, varargs) { + if (EmJVM.debug) { + console.log('EMJVM_CallObjectMethod: ' + [jobject, EmJVM.objects[jobject].name, methodId, EmJVM.objects[jobject]['methodName__' + methodId]]); + } + return EmJVM.objects[jobject]['method__' + methodId](varargs); +} + +function _emjvm_callStaticObjectMethod(jniEnv, jclass, methodId, varargs) { + // Pretend this to be the same as calling a non-static method + return _emjvm_callObjectMethod(jniEnv, jclass, methodId, varargs); +} + +function _emjvm_callStaticBooleanMethod(jniEnv, jclass, methodId, varargs) { + // Only differs in return type + return _emjvm_callStaticObjectMethod(jniEnv, jclass, methodId, varargs); +} + +function _emjvm_callBooleanMethod(jniEnv, jobject, methodId, varargs) { + // Pretend this to be the same as calling a non-static method + return _emjvm_callStaticBooleanMethod(jniEnv, jobject, methodId, varargs); +} + +function _emjvm_callVoidMethod(jniEnv, jobject, methodId, varargs) { + _emjvm_callObjectMethod(jniEnv, jobject, methodId, varargs); +} + +function _emjvm_callIntMethod(jniEnv, jobject, methodId, varargs) { + return _emjvm_callObjectMethod(jniEnv, jobject, methodId, varargs); +} + +function _emjvm_deleteLocalRef(jniEnv, jobject) { + if (EmJVM.debug) { + console.log('EMJVM_DeleteLocalRef: ' + [jobject]); + } + var obj = EmJVM.objects[jobject]; + obj.refs--; + if (obj.refs == 0) { + if (EmJVM.debug) { + console.log('EMJVM_DeleteLocalRef: remove ' + obj.name); + } + delete EmJVM.objects[jobject]; + } +} + +function _emjvm_getArrayLength(jniEnv, jobject) { + var obj = EmJVM.objects[jobject]; + assert(obj.name == 'byteArray'); + return obj.value.length; +} + +function _emjvm_getByteArrayRegion(jniEnv, jobject, start, len, buf) { + var obj = EmJVM.objects[jobject]; + assert(obj.name == 'byteArray'); + assert(obj.value); // we set this to null below and assume we are never called again + if (EmJVM.debug) { + console.log('emjvm_getByteArrayRegion: ' + [jobject, obj.value.length, start, len, buf]); + } + assert(start + len <= obj.value.length); + assert(len == obj.value.length); // we assume users read it all, and we can now copy it all with set() and then free it + HEAPU8.set(obj.value, buf); + obj.value = null; // XXX assume byte arrays are one-shot +} + +function _emjvm_findClass(env, name) { + name = Pointer_stringify(name); + if (EmJVM.debug) { + console.log('emjvm_findClass: ' + [name]); + } + var obj = EmJVM.classNames[name]; + assert(obj); + return obj.id; +} + diff --git a/third_party/jni/jni.h b/third_party/jni/jni.h new file mode 100644 index 00000000..b425dd42 --- /dev/null +++ b/third_party/jni/jni.h @@ -0,0 +1,1154 @@ +/* +* Copyright (C) 2006 The Android Open Source Project +* +* 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. +*/ + +/* + * JNI specification, as defined by Sun: + * http://java.sun.com/javase/6/docs/technotes/guides/jni/spec/jniTOC.html + * + * Everything here is expected to be VM-neutral. + */ +#ifndef _JNI_H +#define _JNI_H + +#include <stdarg.h> + +/* + * Primitive types that match up with Java equivalents. + */ +#ifdef HAVE_INTTYPES_H +# include <inttypes.h> /* C99 */ +typedef uint8_t jboolean; /* unsigned 8 bits */ +typedef int8_t jbyte; /* signed 8 bits */ +typedef uint16_t jchar; /* unsigned 16 bits */ +typedef int16_t jshort; /* signed 16 bits */ +typedef int32_t jint; /* signed 32 bits */ +typedef int64_t jlong; /* signed 64 bits */ +typedef float jfloat; /* 32-bit IEEE 754 */ +typedef double jdouble; /* 64-bit IEEE 754 */ +#else +typedef unsigned char jboolean; /* unsigned 8 bits */ +typedef signed char jbyte; /* signed 8 bits */ +typedef unsigned short jchar; /* unsigned 16 bits */ +typedef short jshort; /* signed 16 bits */ +typedef int jint; /* signed 32 bits */ +typedef long long jlong; /* signed 64 bits */ +typedef float jfloat; /* 32-bit IEEE 754 */ +typedef double jdouble; /* 64-bit IEEE 754 */ +#endif + +/* "cardinal indices and sizes" */ +typedef jint jsize; + +#ifdef __cplusplus +/* + * Reference types, in C++ + */ +class _jobject {}; +class _jclass : public _jobject {}; +class _jstring : public _jobject {}; +class _jarray : public _jobject {}; +class _jobjectArray : public _jarray {}; +class _jbooleanArray : public _jarray {}; +class _jbyteArray : public _jarray {}; +class _jcharArray : public _jarray {}; +class _jshortArray : public _jarray {}; +class _jintArray : public _jarray {}; +class _jlongArray : public _jarray {}; +class _jfloatArray : public _jarray {}; +class _jdoubleArray : public _jarray {}; +class _jthrowable : public _jobject {}; + +typedef _jobject* jobject; +typedef _jclass* jclass; +typedef _jstring* jstring; +typedef _jarray* jarray; +typedef _jobjectArray* jobjectArray; +typedef _jbooleanArray* jbooleanArray; +typedef _jbyteArray* jbyteArray; +typedef _jcharArray* jcharArray; +typedef _jshortArray* jshortArray; +typedef _jintArray* jintArray; +typedef _jlongArray* jlongArray; +typedef _jfloatArray* jfloatArray; +typedef _jdoubleArray* jdoubleArray; +typedef _jthrowable* jthrowable; +typedef _jobject* jweak; + + +#else /* not __cplusplus */ + +/* + * Reference types, in C. + */ +typedef void* jobject; +typedef jobject jclass; +typedef jobject jstring; +typedef jobject jarray; +typedef jarray jobjectArray; +typedef jarray jbooleanArray; +typedef jarray jbyteArray; +typedef jarray jcharArray; +typedef jarray jshortArray; +typedef jarray jintArray; +typedef jarray jlongArray; +typedef jarray jfloatArray; +typedef jarray jdoubleArray; +typedef jobject jthrowable; +typedef jobject jweak; + +#endif /* not __cplusplus */ + +struct _jfieldID; /* opaque structure */ +typedef struct _jfieldID* jfieldID; /* field IDs */ + +struct _jmethodID; /* opaque structure */ +typedef struct _jmethodID* jmethodID; /* method IDs */ + +struct JNIInvokeInterface; + +typedef union jvalue { + jboolean z; + jbyte b; + jchar c; + jshort s; + jint i; + jlong j; + jfloat f; + jdouble d; + jobject l; +} jvalue; + +typedef enum jobjectRefType { + JNIInvalidRefType = 0, + JNILocalRefType = 1, + JNIGlobalRefType = 2, + JNIWeakGlobalRefType = 3 +} jobjectRefType; + +typedef struct { + const char* name; + const char* signature; + void* fnPtr; +} JNINativeMethod; + +struct _JNIEnv; +struct _JavaVM; +typedef const struct JNINativeInterface* C_JNIEnv; + +#if defined(__cplusplus) +typedef _JNIEnv JNIEnv; +typedef _JavaVM JavaVM; +#else +typedef const struct JNINativeInterface* JNIEnv; +typedef const struct JNIInvokeInterface* JavaVM; +#endif + +/* + * Table of interface function pointers. + */ +struct JNINativeInterface { + void* reserved0; + void* reserved1; + void* reserved2; + void* reserved3; + + jint (*GetVersion)(JNIEnv *); + + jclass (*DefineClass)(JNIEnv*, const char*, jobject, const jbyte*, + jsize); + jclass (*FindClass)(JNIEnv*, const char*); + + jmethodID (*FromReflectedMethod)(JNIEnv*, jobject); + jfieldID (*FromReflectedField)(JNIEnv*, jobject); + /* spec doesn't show jboolean parameter */ + jobject (*ToReflectedMethod)(JNIEnv*, jclass, jmethodID, jboolean); + + jclass (*GetSuperclass)(JNIEnv*, jclass); + jboolean (*IsAssignableFrom)(JNIEnv*, jclass, jclass); + + /* spec doesn't show jboolean parameter */ + jobject (*ToReflectedField)(JNIEnv*, jclass, jfieldID, jboolean); + + jint (*Throw)(JNIEnv*, jthrowable); + jint (*ThrowNew)(JNIEnv *, jclass, const char *); + jthrowable (*ExceptionOccurred)(JNIEnv*); + void (*ExceptionDescribe)(JNIEnv*); + void (*ExceptionClear)(JNIEnv*); + void (*FatalError)(JNIEnv*, const char*); + + jint (*PushLocalFrame)(JNIEnv*, jint); + jobject (*PopLocalFrame)(JNIEnv*, jobject); + + jobject (*NewGlobalRef)(JNIEnv*, jobject); + void (*DeleteGlobalRef)(JNIEnv*, jobject); + void (*DeleteLocalRef)(JNIEnv*, jobject); + jboolean (*IsSameObject)(JNIEnv*, jobject, jobject); + + jobject (*NewLocalRef)(JNIEnv*, jobject); + jint (*EnsureLocalCapacity)(JNIEnv*, jint); + + jobject (*AllocObject)(JNIEnv*, jclass); + jobject (*NewObject)(JNIEnv*, jclass, jmethodID, ...); + jobject (*NewObjectV)(JNIEnv*, jclass, jmethodID, va_list); + jobject (*NewObjectA)(JNIEnv*, jclass, jmethodID, jvalue*); + + jclass (*GetObjectClass)(JNIEnv*, jobject); + jboolean (*IsInstanceOf)(JNIEnv*, jobject, jclass); + jmethodID (*GetMethodID)(JNIEnv*, jclass, const char*, const char*); + + jobject (*CallObjectMethod)(JNIEnv*, jobject, jmethodID, ...); + jobject (*CallObjectMethodV)(JNIEnv*, jobject, jmethodID, va_list); + jobject (*CallObjectMethodA)(JNIEnv*, jobject, jmethodID, jvalue*); + jboolean (*CallBooleanMethod)(JNIEnv*, jobject, jmethodID, ...); + jboolean (*CallBooleanMethodV)(JNIEnv*, jobject, jmethodID, va_list); + jboolean (*CallBooleanMethodA)(JNIEnv*, jobject, jmethodID, jvalue*); + jbyte (*CallByteMethod)(JNIEnv*, jobject, jmethodID, ...); + jbyte (*CallByteMethodV)(JNIEnv*, jobject, jmethodID, va_list); + jbyte (*CallByteMethodA)(JNIEnv*, jobject, jmethodID, jvalue*); + jchar (*CallCharMethod)(JNIEnv*, jobject, jmethodID, ...); + jchar (*CallCharMethodV)(JNIEnv*, jobject, jmethodID, va_list); + jchar (*CallCharMethodA)(JNIEnv*, jobject, jmethodID, jvalue*); + jshort (*CallShortMethod)(JNIEnv*, jobject, jmethodID, ...); + jshort (*CallShortMethodV)(JNIEnv*, jobject, jmethodID, va_list); + jshort (*CallShortMethodA)(JNIEnv*, jobject, jmethodID, jvalue*); + jint (*CallIntMethod)(JNIEnv*, jobject, jmethodID, ...); + jint (*CallIntMethodV)(JNIEnv*, jobject, jmethodID, va_list); + jint (*CallIntMethodA)(JNIEnv*, jobject, jmethodID, jvalue*); + jlong (*CallLongMethod)(JNIEnv*, jobject, jmethodID, ...); + jlong (*CallLongMethodV)(JNIEnv*, jobject, jmethodID, va_list); + jlong (*CallLongMethodA)(JNIEnv*, jobject, jmethodID, jvalue*); + jfloat (*CallFloatMethod)(JNIEnv*, jobject, jmethodID, ...); + jfloat (*CallFloatMethodV)(JNIEnv*, jobject, jmethodID, va_list); + jfloat (*CallFloatMethodA)(JNIEnv*, jobject, jmethodID, jvalue*); + jdouble (*CallDoubleMethod)(JNIEnv*, jobject, jmethodID, ...); + jdouble (*CallDoubleMethodV)(JNIEnv*, jobject, jmethodID, va_list); + jdouble (*CallDoubleMethodA)(JNIEnv*, jobject, jmethodID, jvalue*); + void (*CallVoidMethod)(JNIEnv*, jobject, jmethodID, ...); + void (*CallVoidMethodV)(JNIEnv*, jobject, jmethodID, va_list); + void (*CallVoidMethodA)(JNIEnv*, jobject, jmethodID, jvalue*); + + jobject (*CallNonvirtualObjectMethod)(JNIEnv*, jobject, jclass, + jmethodID, ...); + jobject (*CallNonvirtualObjectMethodV)(JNIEnv*, jobject, jclass, + jmethodID, va_list); + jobject (*CallNonvirtualObjectMethodA)(JNIEnv*, jobject, jclass, + jmethodID, jvalue*); + jboolean (*CallNonvirtualBooleanMethod)(JNIEnv*, jobject, jclass, + jmethodID, ...); + jboolean (*CallNonvirtualBooleanMethodV)(JNIEnv*, jobject, jclass, + jmethodID, va_list); + jboolean (*CallNonvirtualBooleanMethodA)(JNIEnv*, jobject, jclass, + jmethodID, jvalue*); + jbyte (*CallNonvirtualByteMethod)(JNIEnv*, jobject, jclass, + jmethodID, ...); + jbyte (*CallNonvirtualByteMethodV)(JNIEnv*, jobject, jclass, + jmethodID, va_list); + jbyte (*CallNonvirtualByteMethodA)(JNIEnv*, jobject, jclass, + jmethodID, jvalue*); + jchar (*CallNonvirtualCharMethod)(JNIEnv*, jobject, jclass, + jmethodID, ...); + jchar (*CallNonvirtualCharMethodV)(JNIEnv*, jobject, jclass, + jmethodID, va_list); + jchar (*CallNonvirtualCharMethodA)(JNIEnv*, jobject, jclass, + jmethodID, jvalue*); + jshort (*CallNonvirtualShortMethod)(JNIEnv*, jobject, jclass, + jmethodID, ...); + jshort (*CallNonvirtualShortMethodV)(JNIEnv*, jobject, jclass, + jmethodID, va_list); + jshort (*CallNonvirtualShortMethodA)(JNIEnv*, jobject, jclass, + jmethodID, jvalue*); + jint (*CallNonvirtualIntMethod)(JNIEnv*, jobject, jclass, + jmethodID, ...); + jint (*CallNonvirtualIntMethodV)(JNIEnv*, jobject, jclass, + jmethodID, va_list); + jint (*CallNonvirtualIntMethodA)(JNIEnv*, jobject, jclass, + jmethodID, jvalue*); + jlong (*CallNonvirtualLongMethod)(JNIEnv*, jobject, jclass, + jmethodID, ...); + jlong (*CallNonvirtualLongMethodV)(JNIEnv*, jobject, jclass, + jmethodID, va_list); + jlong (*CallNonvirtualLongMethodA)(JNIEnv*, jobject, jclass, + jmethodID, jvalue*); + jfloat (*CallNonvirtualFloatMethod)(JNIEnv*, jobject, jclass, + jmethodID, ...); + jfloat (*CallNonvirtualFloatMethodV)(JNIEnv*, jobject, jclass, + jmethodID, va_list); + jfloat (*CallNonvirtualFloatMethodA)(JNIEnv*, jobject, jclass, + jmethodID, jvalue*); + jdouble (*CallNonvirtualDoubleMethod)(JNIEnv*, jobject, jclass, + jmethodID, ...); + jdouble (*CallNonvirtualDoubleMethodV)(JNIEnv*, jobject, jclass, + jmethodID, va_list); + jdouble (*CallNonvirtualDoubleMethodA)(JNIEnv*, jobject, jclass, + jmethodID, jvalue*); + void (*CallNonvirtualVoidMethod)(JNIEnv*, jobject, jclass, + jmethodID, ...); + void (*CallNonvirtualVoidMethodV)(JNIEnv*, jobject, jclass, + jmethodID, va_list); + void (*CallNonvirtualVoidMethodA)(JNIEnv*, jobject, jclass, + jmethodID, jvalue*); + + jfieldID (*GetFieldID)(JNIEnv*, jclass, const char*, const char*); + + jobject (*GetObjectField)(JNIEnv*, jobject, jfieldID); + jboolean (*GetBooleanField)(JNIEnv*, jobject, jfieldID); + jbyte (*GetByteField)(JNIEnv*, jobject, jfieldID); + jchar (*GetCharField)(JNIEnv*, jobject, jfieldID); + jshort (*GetShortField)(JNIEnv*, jobject, jfieldID); + jint (*GetIntField)(JNIEnv*, jobject, jfieldID); + jlong (*GetLongField)(JNIEnv*, jobject, jfieldID); + jfloat (*GetFloatField)(JNIEnv*, jobject, jfieldID); + jdouble (*GetDoubleField)(JNIEnv*, jobject, jfieldID); + + void (*SetObjectField)(JNIEnv*, jobject, jfieldID, jobject); + void (*SetBooleanField)(JNIEnv*, jobject, jfieldID, jboolean); + void (*SetByteField)(JNIEnv*, jobject, jfieldID, jbyte); + void (*SetCharField)(JNIEnv*, jobject, jfieldID, jchar); + void (*SetShortField)(JNIEnv*, jobject, jfieldID, jshort); + void (*SetIntField)(JNIEnv*, jobject, jfieldID, jint); + void (*SetLongField)(JNIEnv*, jobject, jfieldID, jlong); + void (*SetFloatField)(JNIEnv*, jobject, jfieldID, jfloat); + void (*SetDoubleField)(JNIEnv*, jobject, jfieldID, jdouble); + + jmethodID (*GetStaticMethodID)(JNIEnv*, jclass, const char*, const char*); + + jobject (*CallStaticObjectMethod)(JNIEnv*, jclass, jmethodID, ...); + jobject (*CallStaticObjectMethodV)(JNIEnv*, jclass, jmethodID, va_list); + jobject (*CallStaticObjectMethodA)(JNIEnv*, jclass, jmethodID, jvalue*); + jboolean (*CallStaticBooleanMethod)(JNIEnv*, jclass, jmethodID, ...); + jboolean (*CallStaticBooleanMethodV)(JNIEnv*, jclass, jmethodID, + va_list); + jboolean (*CallStaticBooleanMethodA)(JNIEnv*, jclass, jmethodID, + jvalue*); + jbyte (*CallStaticByteMethod)(JNIEnv*, jclass, jmethodID, ...); + jbyte (*CallStaticByteMethodV)(JNIEnv*, jclass, jmethodID, va_list); + jbyte (*CallStaticByteMethodA)(JNIEnv*, jclass, jmethodID, jvalue*); + jchar (*CallStaticCharMethod)(JNIEnv*, jclass, jmethodID, ...); + jchar (*CallStaticCharMethodV)(JNIEnv*, jclass, jmethodID, va_list); + jchar (*CallStaticCharMethodA)(JNIEnv*, jclass, jmethodID, jvalue*); + jshort (*CallStaticShortMethod)(JNIEnv*, jclass, jmethodID, ...); + jshort (*CallStaticShortMethodV)(JNIEnv*, jclass, jmethodID, va_list); + jshort (*CallStaticShortMethodA)(JNIEnv*, jclass, jmethodID, jvalue*); + jint (*CallStaticIntMethod)(JNIEnv*, jclass, jmethodID, ...); + jint (*CallStaticIntMethodV)(JNIEnv*, jclass, jmethodID, va_list); + jint (*CallStaticIntMethodA)(JNIEnv*, jclass, jmethodID, jvalue*); + jlong (*CallStaticLongMethod)(JNIEnv*, jclass, jmethodID, ...); + jlong (*CallStaticLongMethodV)(JNIEnv*, jclass, jmethodID, va_list); + jlong (*CallStaticLongMethodA)(JNIEnv*, jclass, jmethodID, jvalue*); + jfloat (*CallStaticFloatMethod)(JNIEnv*, jclass, jmethodID, ...); + jfloat (*CallStaticFloatMethodV)(JNIEnv*, jclass, jmethodID, va_list); + jfloat (*CallStaticFloatMethodA)(JNIEnv*, jclass, jmethodID, jvalue*); + jdouble (*CallStaticDoubleMethod)(JNIEnv*, jclass, jmethodID, ...); + jdouble (*CallStaticDoubleMethodV)(JNIEnv*, jclass, jmethodID, va_list); + jdouble (*CallStaticDoubleMethodA)(JNIEnv*, jclass, jmethodID, jvalue*); + void (*CallStaticVoidMethod)(JNIEnv*, jclass, jmethodID, ...); + void (*CallStaticVoidMethodV)(JNIEnv*, jclass, jmethodID, va_list); + void (*CallStaticVoidMethodA)(JNIEnv*, jclass, jmethodID, jvalue*); + + jfieldID (*GetStaticFieldID)(JNIEnv*, jclass, const char*, + const char*); + + jobject (*GetStaticObjectField)(JNIEnv*, jclass, jfieldID); + jboolean (*GetStaticBooleanField)(JNIEnv*, jclass, jfieldID); + jbyte (*GetStaticByteField)(JNIEnv*, jclass, jfieldID); + jchar (*GetStaticCharField)(JNIEnv*, jclass, jfieldID); + jshort (*GetStaticShortField)(JNIEnv*, jclass, jfieldID); + jint (*GetStaticIntField)(JNIEnv*, jclass, jfieldID); + jlong (*GetStaticLongField)(JNIEnv*, jclass, jfieldID); + jfloat (*GetStaticFloatField)(JNIEnv*, jclass, jfieldID); + jdouble (*GetStaticDoubleField)(JNIEnv*, jclass, jfieldID); + + void (*SetStaticObjectField)(JNIEnv*, jclass, jfieldID, jobject); + void (*SetStaticBooleanField)(JNIEnv*, jclass, jfieldID, jboolean); + void (*SetStaticByteField)(JNIEnv*, jclass, jfieldID, jbyte); + void (*SetStaticCharField)(JNIEnv*, jclass, jfieldID, jchar); + void (*SetStaticShortField)(JNIEnv*, jclass, jfieldID, jshort); + void (*SetStaticIntField)(JNIEnv*, jclass, jfieldID, jint); + void (*SetStaticLongField)(JNIEnv*, jclass, jfieldID, jlong); + void (*SetStaticFloatField)(JNIEnv*, jclass, jfieldID, jfloat); + void (*SetStaticDoubleField)(JNIEnv*, jclass, jfieldID, jdouble); + + jstring (*NewString)(JNIEnv*, const jchar*, jsize); + jsize (*GetStringLength)(JNIEnv*, jstring); + const jchar* (*GetStringChars)(JNIEnv*, jstring, jboolean*); + void (*ReleaseStringChars)(JNIEnv*, jstring, const jchar*); + jstring (*NewStringUTF)(JNIEnv*, const char*); + jsize (*GetStringUTFLength)(JNIEnv*, jstring); + /* JNI spec says this returns const jbyte*, but that's inconsistent */ + const char* (*GetStringUTFChars)(JNIEnv*, jstring, jboolean*); + void (*ReleaseStringUTFChars)(JNIEnv*, jstring, const char*); + jsize (*GetArrayLength)(JNIEnv*, jarray); + jobjectArray (*NewObjectArray)(JNIEnv*, jsize, jclass, jobject); + jobject (*GetObjectArrayElement)(JNIEnv*, jobjectArray, jsize); + void (*SetObjectArrayElement)(JNIEnv*, jobjectArray, jsize, jobject); + + jbooleanArray (*NewBooleanArray)(JNIEnv*, jsize); + jbyteArray (*NewByteArray)(JNIEnv*, jsize); + jcharArray (*NewCharArray)(JNIEnv*, jsize); + jshortArray (*NewShortArray)(JNIEnv*, jsize); + jintArray (*NewIntArray)(JNIEnv*, jsize); + jlongArray (*NewLongArray)(JNIEnv*, jsize); + jfloatArray (*NewFloatArray)(JNIEnv*, jsize); + jdoubleArray (*NewDoubleArray)(JNIEnv*, jsize); + + jboolean* (*GetBooleanArrayElements)(JNIEnv*, jbooleanArray, jboolean*); + jbyte* (*GetByteArrayElements)(JNIEnv*, jbyteArray, jboolean*); + jchar* (*GetCharArrayElements)(JNIEnv*, jcharArray, jboolean*); + jshort* (*GetShortArrayElements)(JNIEnv*, jshortArray, jboolean*); + jint* (*GetIntArrayElements)(JNIEnv*, jintArray, jboolean*); + jlong* (*GetLongArrayElements)(JNIEnv*, jlongArray, jboolean*); + jfloat* (*GetFloatArrayElements)(JNIEnv*, jfloatArray, jboolean*); + jdouble* (*GetDoubleArrayElements)(JNIEnv*, jdoubleArray, jboolean*); + + void (*ReleaseBooleanArrayElements)(JNIEnv*, jbooleanArray, + jboolean*, jint); + void (*ReleaseByteArrayElements)(JNIEnv*, jbyteArray, + jbyte*, jint); + void (*ReleaseCharArrayElements)(JNIEnv*, jcharArray, + jchar*, jint); + void (*ReleaseShortArrayElements)(JNIEnv*, jshortArray, + jshort*, jint); + void (*ReleaseIntArrayElements)(JNIEnv*, jintArray, + jint*, jint); + void (*ReleaseLongArrayElements)(JNIEnv*, jlongArray, + jlong*, jint); + void (*ReleaseFloatArrayElements)(JNIEnv*, jfloatArray, + jfloat*, jint); + void (*ReleaseDoubleArrayElements)(JNIEnv*, jdoubleArray, + jdouble*, jint); + + void (*GetBooleanArrayRegion)(JNIEnv*, jbooleanArray, + jsize, jsize, jboolean*); + void (*GetByteArrayRegion)(JNIEnv*, jbyteArray, + jsize, jsize, jbyte*); + void (*GetCharArrayRegion)(JNIEnv*, jcharArray, + jsize, jsize, jchar*); + void (*GetShortArrayRegion)(JNIEnv*, jshortArray, + jsize, jsize, jshort*); + void (*GetIntArrayRegion)(JNIEnv*, jintArray, + jsize, jsize, jint*); + void (*GetLongArrayRegion)(JNIEnv*, jlongArray, + jsize, jsize, jlong*); + void (*GetFloatArrayRegion)(JNIEnv*, jfloatArray, + jsize, jsize, jfloat*); + void (*GetDoubleArrayRegion)(JNIEnv*, jdoubleArray, + jsize, jsize, jdouble*); + + /* spec shows these without const; some jni.h do, some don't */ + void (*SetBooleanArrayRegion)(JNIEnv*, jbooleanArray, + jsize, jsize, const jboolean*); + void (*SetByteArrayRegion)(JNIEnv*, jbyteArray, + jsize, jsize, const jbyte*); + void (*SetCharArrayRegion)(JNIEnv*, jcharArray, + jsize, jsize, const jchar*); + void (*SetShortArrayRegion)(JNIEnv*, jshortArray, + jsize, jsize, const jshort*); + void (*SetIntArrayRegion)(JNIEnv*, jintArray, + jsize, jsize, const jint*); + void (*SetLongArrayRegion)(JNIEnv*, jlongArray, + jsize, jsize, const jlong*); + void (*SetFloatArrayRegion)(JNIEnv*, jfloatArray, + jsize, jsize, const jfloat*); + void (*SetDoubleArrayRegion)(JNIEnv*, jdoubleArray, + jsize, jsize, const jdouble*); + + jint (*RegisterNatives)(JNIEnv*, jclass, const JNINativeMethod*, + jint); + jint (*UnregisterNatives)(JNIEnv*, jclass); + jint (*MonitorEnter)(JNIEnv*, jobject); + jint (*MonitorExit)(JNIEnv*, jobject); + jint (*GetJavaVM)(JNIEnv*, JavaVM**); + + void (*GetStringRegion)(JNIEnv*, jstring, jsize, jsize, jchar*); + void (*GetStringUTFRegion)(JNIEnv*, jstring, jsize, jsize, char*); + + void* (*GetPrimitiveArrayCritical)(JNIEnv*, jarray, jboolean*); + void (*ReleasePrimitiveArrayCritical)(JNIEnv*, jarray, void*, jint); + + const jchar* (*GetStringCritical)(JNIEnv*, jstring, jboolean*); + void (*ReleaseStringCritical)(JNIEnv*, jstring, const jchar*); + + jweak (*NewWeakGlobalRef)(JNIEnv*, jobject); + void (*DeleteWeakGlobalRef)(JNIEnv*, jweak); + + jboolean (*ExceptionCheck)(JNIEnv*); + + jobject (*NewDirectByteBuffer)(JNIEnv*, void*, jlong); + void* (*GetDirectBufferAddress)(JNIEnv*, jobject); + jlong (*GetDirectBufferCapacity)(JNIEnv*, jobject); + + /* added in JNI 1.6 */ + jobjectRefType (*GetObjectRefType)(JNIEnv*, jobject); +}; + +/* + * C++ object wrapper. + * + * This is usually overlaid on a C struct whose first element is a + * JNINativeInterface*. We rely somewhat on compiler behavior. + */ +struct _JNIEnv { + /* do not rename this; it does not seem to be entirely opaque */ + const struct JNINativeInterface* functions; + +#if defined(__cplusplus) + + jint GetVersion() + { return functions->GetVersion(this); } + + jclass DefineClass(const char *name, jobject loader, const jbyte* buf, + jsize bufLen) + { return functions->DefineClass(this, name, loader, buf, bufLen); } + + jclass FindClass(const char* name) + { return functions->FindClass(this, name); } + + jmethodID FromReflectedMethod(jobject method) + { return functions->FromReflectedMethod(this, method); } + + jfieldID FromReflectedField(jobject field) + { return functions->FromReflectedField(this, field); } + + jobject ToReflectedMethod(jclass cls, jmethodID methodID, jboolean isStatic) + { return functions->ToReflectedMethod(this, cls, methodID, isStatic); } + + jclass GetSuperclass(jclass clazz) + { return functions->GetSuperclass(this, clazz); } + + jboolean IsAssignableFrom(jclass clazz1, jclass clazz2) + { return functions->IsAssignableFrom(this, clazz1, clazz2); } + + jobject ToReflectedField(jclass cls, jfieldID fieldID, jboolean isStatic) + { return functions->ToReflectedField(this, cls, fieldID, isStatic); } + + jint Throw(jthrowable obj) + { return functions->Throw(this, obj); } + + jint ThrowNew(jclass clazz, const char* message) + { return functions->ThrowNew(this, clazz, message); } + + jthrowable ExceptionOccurred() + { return functions->ExceptionOccurred(this); } + + void ExceptionDescribe() + { functions->ExceptionDescribe(this); } + + void ExceptionClear() + { functions->ExceptionClear(this); } + + void FatalError(const char* msg) + { functions->FatalError(this, msg); } + + jint PushLocalFrame(jint capacity) + { return functions->PushLocalFrame(this, capacity); } + + jobject PopLocalFrame(jobject result) + { return functions->PopLocalFrame(this, result); } + + jobject NewGlobalRef(jobject obj) + { return functions->NewGlobalRef(this, obj); } + + void DeleteGlobalRef(jobject globalRef) + { functions->DeleteGlobalRef(this, globalRef); } + + void DeleteLocalRef(jobject localRef) + { functions->DeleteLocalRef(this, localRef); } + + jboolean IsSameObject(jobject ref1, jobject ref2) + { return functions->IsSameObject(this, ref1, ref2); } + + jobject NewLocalRef(jobject ref) + { return functions->NewLocalRef(this, ref); } + + jint EnsureLocalCapacity(jint capacity) + { return functions->EnsureLocalCapacity(this, capacity); } + + jobject AllocObject(jclass clazz) + { return functions->AllocObject(this, clazz); } + + jobject NewObject(jclass clazz, jmethodID methodID, ...) + { + va_list args; + va_start(args, methodID); + jobject result = functions->NewObjectV(this, clazz, methodID, args); + va_end(args); + return result; + } + + jobject NewObjectV(jclass clazz, jmethodID methodID, va_list args) + { return functions->NewObjectV(this, clazz, methodID, args); } + + jobject NewObjectA(jclass clazz, jmethodID methodID, jvalue* args) + { return functions->NewObjectA(this, clazz, methodID, args); } + + jclass GetObjectClass(jobject obj) + { return functions->GetObjectClass(this, obj); } + + jboolean IsInstanceOf(jobject obj, jclass clazz) + { return functions->IsInstanceOf(this, obj, clazz); } + + jmethodID GetMethodID(jclass clazz, const char* name, const char* sig) + { return functions->GetMethodID(this, clazz, name, sig); } + +#define CALL_TYPE_METHOD(_jtype, _jname) \ + _jtype Call##_jname##Method(jobject obj, jmethodID methodID, ...) \ + { \ + _jtype result; \ + va_list args; \ + va_start(args, methodID); \ + result = functions->Call##_jname##MethodV(this, obj, methodID, \ + args); \ + va_end(args); \ + return result; \ + } +#define CALL_TYPE_METHODV(_jtype, _jname) \ + _jtype Call##_jname##MethodV(jobject obj, jmethodID methodID, \ + va_list args) \ + { return functions->Call##_jname##MethodV(this, obj, methodID, args); } +#define CALL_TYPE_METHODA(_jtype, _jname) \ + _jtype Call##_jname##MethodA(jobject obj, jmethodID methodID, \ + jvalue* args) \ + { return functions->Call##_jname##MethodA(this, obj, methodID, args); } + +#define CALL_TYPE(_jtype, _jname) \ + CALL_TYPE_METHOD(_jtype, _jname) \ + CALL_TYPE_METHODV(_jtype, _jname) \ + CALL_TYPE_METHODA(_jtype, _jname) + + CALL_TYPE(jobject, Object) + CALL_TYPE(jboolean, Boolean) + CALL_TYPE(jbyte, Byte) + CALL_TYPE(jchar, Char) + CALL_TYPE(jshort, Short) + CALL_TYPE(jint, Int) + CALL_TYPE(jlong, Long) + CALL_TYPE(jfloat, Float) + CALL_TYPE(jdouble, Double) + + void CallVoidMethod(jobject obj, jmethodID methodID, ...) + { + va_list args; + va_start(args, methodID); + functions->CallVoidMethodV(this, obj, methodID, args); + va_end(args); + } + void CallVoidMethodV(jobject obj, jmethodID methodID, va_list args) + { functions->CallVoidMethodV(this, obj, methodID, args); } + void CallVoidMethodA(jobject obj, jmethodID methodID, jvalue* args) + { functions->CallVoidMethodA(this, obj, methodID, args); } + +#define CALL_NONVIRT_TYPE_METHOD(_jtype, _jname) \ + _jtype CallNonvirtual##_jname##Method(jobject obj, jclass clazz, \ + jmethodID methodID, ...) \ + { \ + _jtype result; \ + va_list args; \ + va_start(args, methodID); \ + result = functions->CallNonvirtual##_jname##MethodV(this, obj, \ + clazz, methodID, args); \ + va_end(args); \ + return result; \ + } +#define CALL_NONVIRT_TYPE_METHODV(_jtype, _jname) \ + _jtype CallNonvirtual##_jname##MethodV(jobject obj, jclass clazz, \ + jmethodID methodID, va_list args) \ + { return functions->CallNonvirtual##_jname##MethodV(this, obj, clazz, \ + methodID, args); } +#define CALL_NONVIRT_TYPE_METHODA(_jtype, _jname) \ + _jtype CallNonvirtual##_jname##MethodA(jobject obj, jclass clazz, \ + jmethodID methodID, jvalue* args) \ + { return functions->CallNonvirtual##_jname##MethodA(this, obj, clazz, \ + methodID, args); } + +#define CALL_NONVIRT_TYPE(_jtype, _jname) \ + CALL_NONVIRT_TYPE_METHOD(_jtype, _jname) \ + CALL_NONVIRT_TYPE_METHODV(_jtype, _jname) \ + CALL_NONVIRT_TYPE_METHODA(_jtype, _jname) + + CALL_NONVIRT_TYPE(jobject, Object) + CALL_NONVIRT_TYPE(jboolean, Boolean) + CALL_NONVIRT_TYPE(jbyte, Byte) + CALL_NONVIRT_TYPE(jchar, Char) + CALL_NONVIRT_TYPE(jshort, Short) + CALL_NONVIRT_TYPE(jint, Int) + CALL_NONVIRT_TYPE(jlong, Long) + CALL_NONVIRT_TYPE(jfloat, Float) + CALL_NONVIRT_TYPE(jdouble, Double) + + void CallNonvirtualVoidMethod(jobject obj, jclass clazz, + jmethodID methodID, ...) + { + va_list args; + va_start(args, methodID); + functions->CallNonvirtualVoidMethodV(this, obj, clazz, methodID, args); + va_end(args); + } + void CallNonvirtualVoidMethodV(jobject obj, jclass clazz, + jmethodID methodID, va_list args) + { functions->CallNonvirtualVoidMethodV(this, obj, clazz, methodID, args); } + void CallNonvirtualVoidMethodA(jobject obj, jclass clazz, + jmethodID methodID, jvalue* args) + { functions->CallNonvirtualVoidMethodA(this, obj, clazz, methodID, args); } + + jfieldID GetFieldID(jclass clazz, const char* name, const char* sig) + { return functions->GetFieldID(this, clazz, name, sig); } + + jobject GetObjectField(jobject obj, jfieldID fieldID) + { return functions->GetObjectField(this, obj, fieldID); } + jboolean GetBooleanField(jobject obj, jfieldID fieldID) + { return functions->GetBooleanField(this, obj, fieldID); } + jbyte GetByteField(jobject obj, jfieldID fieldID) + { return functions->GetByteField(this, obj, fieldID); } + jchar GetCharField(jobject obj, jfieldID fieldID) + { return functions->GetCharField(this, obj, fieldID); } + jshort GetShortField(jobject obj, jfieldID fieldID) + { return functions->GetShortField(this, obj, fieldID); } + jint GetIntField(jobject obj, jfieldID fieldID) + { return functions->GetIntField(this, obj, fieldID); } + jlong GetLongField(jobject obj, jfieldID fieldID) + { return functions->GetLongField(this, obj, fieldID); } + jfloat GetFloatField(jobject obj, jfieldID fieldID) + { return functions->GetFloatField(this, obj, fieldID); } + jdouble GetDoubleField(jobject obj, jfieldID fieldID) + { return functions->GetDoubleField(this, obj, fieldID); } + + void SetObjectField(jobject obj, jfieldID fieldID, jobject value) + { functions->SetObjectField(this, obj, fieldID, value); } + void SetBooleanField(jobject obj, jfieldID fieldID, jboolean value) + { functions->SetBooleanField(this, obj, fieldID, value); } + void SetByteField(jobject obj, jfieldID fieldID, jbyte value) + { functions->SetByteField(this, obj, fieldID, value); } + void SetCharField(jobject obj, jfieldID fieldID, jchar value) + { functions->SetCharField(this, obj, fieldID, value); } + void SetShortField(jobject obj, jfieldID fieldID, jshort value) + { functions->SetShortField(this, obj, fieldID, value); } + void SetIntField(jobject obj, jfieldID fieldID, jint value) + { functions->SetIntField(this, obj, fieldID, value); } + void SetLongField(jobject obj, jfieldID fieldID, jlong value) + { functions->SetLongField(this, obj, fieldID, value); } + void SetFloatField(jobject obj, jfieldID fieldID, jfloat value) + { functions->SetFloatField(this, obj, fieldID, value); } + void SetDoubleField(jobject obj, jfieldID fieldID, jdouble value) + { functions->SetDoubleField(this, obj, fieldID, value); } + + jmethodID GetStaticMethodID(jclass clazz, const char* name, const char* sig) + { return functions->GetStaticMethodID(this, clazz, name, sig); } + +#define CALL_STATIC_TYPE_METHOD(_jtype, _jname) \ + _jtype CallStatic##_jname##Method(jclass clazz, jmethodID methodID, \ + ...) \ + { \ + _jtype result; \ + va_list args; \ + va_start(args, methodID); \ + result = functions->CallStatic##_jname##MethodV(this, clazz, \ + methodID, args); \ + va_end(args); \ + return result; \ + } +#define CALL_STATIC_TYPE_METHODV(_jtype, _jname) \ + _jtype CallStatic##_jname##MethodV(jclass clazz, jmethodID methodID, \ + va_list args) \ + { return functions->CallStatic##_jname##MethodV(this, clazz, methodID, \ + args); } +#define CALL_STATIC_TYPE_METHODA(_jtype, _jname) \ + _jtype CallStatic##_jname##MethodA(jclass clazz, jmethodID methodID, \ + jvalue* args) \ + { return functions->CallStatic##_jname##MethodA(this, clazz, methodID, \ + args); } + +#define CALL_STATIC_TYPE(_jtype, _jname) \ + CALL_STATIC_TYPE_METHOD(_jtype, _jname) \ + CALL_STATIC_TYPE_METHODV(_jtype, _jname) \ + CALL_STATIC_TYPE_METHODA(_jtype, _jname) + + CALL_STATIC_TYPE(jobject, Object) + CALL_STATIC_TYPE(jboolean, Boolean) + CALL_STATIC_TYPE(jbyte, Byte) + CALL_STATIC_TYPE(jchar, Char) + CALL_STATIC_TYPE(jshort, Short) + CALL_STATIC_TYPE(jint, Int) + CALL_STATIC_TYPE(jlong, Long) + CALL_STATIC_TYPE(jfloat, Float) + CALL_STATIC_TYPE(jdouble, Double) + + void CallStaticVoidMethod(jclass clazz, jmethodID methodID, ...) + { + va_list args; + va_start(args, methodID); + functions->CallStaticVoidMethodV(this, clazz, methodID, args); + va_end(args); + } + void CallStaticVoidMethodV(jclass clazz, jmethodID methodID, va_list args) + { functions->CallStaticVoidMethodV(this, clazz, methodID, args); } + void CallStaticVoidMethodA(jclass clazz, jmethodID methodID, jvalue* args) + { functions->CallStaticVoidMethodA(this, clazz, methodID, args); } + + jfieldID GetStaticFieldID(jclass clazz, const char* name, const char* sig) + { return functions->GetStaticFieldID(this, clazz, name, sig); } + + jobject GetStaticObjectField(jclass clazz, jfieldID fieldID) + { return functions->GetStaticObjectField(this, clazz, fieldID); } + jboolean GetStaticBooleanField(jclass clazz, jfieldID fieldID) + { return functions->GetStaticBooleanField(this, clazz, fieldID); } + jbyte GetStaticByteField(jclass clazz, jfieldID fieldID) + { return functions->GetStaticByteField(this, clazz, fieldID); } + jchar GetStaticCharField(jclass clazz, jfieldID fieldID) + { return functions->GetStaticCharField(this, clazz, fieldID); } + jshort GetStaticShortField(jclass clazz, jfieldID fieldID) + { return functions->GetStaticShortField(this, clazz, fieldID); } + jint GetStaticIntField(jclass clazz, jfieldID fieldID) + { return functions->GetStaticIntField(this, clazz, fieldID); } + jlong GetStaticLongField(jclass clazz, jfieldID fieldID) + { return functions->GetStaticLongField(this, clazz, fieldID); } + jfloat GetStaticFloatField(jclass clazz, jfieldID fieldID) + { return functions->GetStaticFloatField(this, clazz, fieldID); } + jdouble GetStaticDoubleField(jclass clazz, jfieldID fieldID) + { return functions->GetStaticDoubleField(this, clazz, fieldID); } + + void SetStaticObjectField(jclass clazz, jfieldID fieldID, jobject value) + { functions->SetStaticObjectField(this, clazz, fieldID, value); } + void SetStaticBooleanField(jclass clazz, jfieldID fieldID, jboolean value) + { functions->SetStaticBooleanField(this, clazz, fieldID, value); } + void SetStaticByteField(jclass clazz, jfieldID fieldID, jbyte value) + { functions->SetStaticByteField(this, clazz, fieldID, value); } + void SetStaticCharField(jclass clazz, jfieldID fieldID, jchar value) + { functions->SetStaticCharField(this, clazz, fieldID, value); } + void SetStaticShortField(jclass clazz, jfieldID fieldID, jshort value) + { functions->SetStaticShortField(this, clazz, fieldID, value); } + void SetStaticIntField(jclass clazz, jfieldID fieldID, jint value) + { functions->SetStaticIntField(this, clazz, fieldID, value); } + void SetStaticLongField(jclass clazz, jfieldID fieldID, jlong value) + { functions->SetStaticLongField(this, clazz, fieldID, value); } + void SetStaticFloatField(jclass clazz, jfieldID fieldID, jfloat value) + { functions->SetStaticFloatField(this, clazz, fieldID, value); } + void SetStaticDoubleField(jclass clazz, jfieldID fieldID, jdouble value) + { functions->SetStaticDoubleField(this, clazz, fieldID, value); } + + jstring NewString(const jchar* unicodeChars, jsize len) + { return functions->NewString(this, unicodeChars, len); } + + jsize GetStringLength(jstring string) + { return functions->GetStringLength(this, string); } + + const jchar* GetStringChars(jstring string, jboolean* isCopy) + { return functions->GetStringChars(this, string, isCopy); } + + void ReleaseStringChars(jstring string, const jchar* chars) + { functions->ReleaseStringChars(this, string, chars); } + + jstring NewStringUTF(const char* bytes) + { return functions->NewStringUTF(this, bytes); } + + jsize GetStringUTFLength(jstring string) + { return functions->GetStringUTFLength(this, string); } + + const char* GetStringUTFChars(jstring string, jboolean* isCopy) + { return functions->GetStringUTFChars(this, string, isCopy); } + + void ReleaseStringUTFChars(jstring string, const char* utf) + { functions->ReleaseStringUTFChars(this, string, utf); } + + jsize GetArrayLength(jarray array) + { return functions->GetArrayLength(this, array); } + + jobjectArray NewObjectArray(jsize length, jclass elementClass, + jobject initialElement) + { return functions->NewObjectArray(this, length, elementClass, + initialElement); } + + jobject GetObjectArrayElement(jobjectArray array, jsize index) + { return functions->GetObjectArrayElement(this, array, index); } + + void SetObjectArrayElement(jobjectArray array, jsize index, jobject value) + { functions->SetObjectArrayElement(this, array, index, value); } + + jbooleanArray NewBooleanArray(jsize length) + { return functions->NewBooleanArray(this, length); } + jbyteArray NewByteArray(jsize length) + { return functions->NewByteArray(this, length); } + jcharArray NewCharArray(jsize length) + { return functions->NewCharArray(this, length); } + jshortArray NewShortArray(jsize length) + { return functions->NewShortArray(this, length); } + jintArray NewIntArray(jsize length) + { return functions->NewIntArray(this, length); } + jlongArray NewLongArray(jsize length) + { return functions->NewLongArray(this, length); } + jfloatArray NewFloatArray(jsize length) + { return functions->NewFloatArray(this, length); } + jdoubleArray NewDoubleArray(jsize length) + { return functions->NewDoubleArray(this, length); } + + jboolean* GetBooleanArrayElements(jbooleanArray array, jboolean* isCopy) + { return functions->GetBooleanArrayElements(this, array, isCopy); } + jbyte* GetByteArrayElements(jbyteArray array, jboolean* isCopy) + { return functions->GetByteArrayElements(this, array, isCopy); } + jchar* GetCharArrayElements(jcharArray array, jboolean* isCopy) + { return functions->GetCharArrayElements(this, array, isCopy); } + jshort* GetShortArrayElements(jshortArray array, jboolean* isCopy) + { return functions->GetShortArrayElements(this, array, isCopy); } + jint* GetIntArrayElements(jintArray array, jboolean* isCopy) + { return functions->GetIntArrayElements(this, array, isCopy); } + jlong* GetLongArrayElements(jlongArray array, jboolean* isCopy) + { return functions->GetLongArrayElements(this, array, isCopy); } + jfloat* GetFloatArrayElements(jfloatArray array, jboolean* isCopy) + { return functions->GetFloatArrayElements(this, array, isCopy); } + jdouble* GetDoubleArrayElements(jdoubleArray array, jboolean* isCopy) + { return functions->GetDoubleArrayElements(this, array, isCopy); } + + void ReleaseBooleanArrayElements(jbooleanArray array, jboolean* elems, + jint mode) + { functions->ReleaseBooleanArrayElements(this, array, elems, mode); } + void ReleaseByteArrayElements(jbyteArray array, jbyte* elems, + jint mode) + { functions->ReleaseByteArrayElements(this, array, elems, mode); } + void ReleaseCharArrayElements(jcharArray array, jchar* elems, + jint mode) + { functions->ReleaseCharArrayElements(this, array, elems, mode); } + void ReleaseShortArrayElements(jshortArray array, jshort* elems, + jint mode) + { functions->ReleaseShortArrayElements(this, array, elems, mode); } + void ReleaseIntArrayElements(jintArray array, jint* elems, + jint mode) + { functions->ReleaseIntArrayElements(this, array, elems, mode); } + void ReleaseLongArrayElements(jlongArray array, jlong* elems, + jint mode) + { functions->ReleaseLongArrayElements(this, array, elems, mode); } + void ReleaseFloatArrayElements(jfloatArray array, jfloat* elems, + jint mode) + { functions->ReleaseFloatArrayElements(this, array, elems, mode); } + void ReleaseDoubleArrayElements(jdoubleArray array, jdouble* elems, + jint mode) + { functions->ReleaseDoubleArrayElements(this, array, elems, mode); } + + void GetBooleanArrayRegion(jbooleanArray array, jsize start, jsize len, + jboolean* buf) + { functions->GetBooleanArrayRegion(this, array, start, len, buf); } + void GetByteArrayRegion(jbyteArray array, jsize start, jsize len, + jbyte* buf) + { functions->GetByteArrayRegion(this, array, start, len, buf); } + void GetCharArrayRegion(jcharArray array, jsize start, jsize len, + jchar* buf) + { functions->GetCharArrayRegion(this, array, start, len, buf); } + void GetShortArrayRegion(jshortArray array, jsize start, jsize len, + jshort* buf) + { functions->GetShortArrayRegion(this, array, start, len, buf); } + void GetIntArrayRegion(jintArray array, jsize start, jsize len, + jint* buf) + { functions->GetIntArrayRegion(this, array, start, len, buf); } + void GetLongArrayRegion(jlongArray array, jsize start, jsize len, + jlong* buf) + { functions->GetLongArrayRegion(this, array, start, len, buf); } + void GetFloatArrayRegion(jfloatArray array, jsize start, jsize len, + jfloat* buf) + { functions->GetFloatArrayRegion(this, array, start, len, buf); } + void GetDoubleArrayRegion(jdoubleArray array, jsize start, jsize len, + jdouble* buf) + { functions->GetDoubleArrayRegion(this, array, start, len, buf); } + + void SetBooleanArrayRegion(jbooleanArray array, jsize start, jsize len, + const jboolean* buf) + { functions->SetBooleanArrayRegion(this, array, start, len, buf); } + void SetByteArrayRegion(jbyteArray array, jsize start, jsize len, + const jbyte* buf) + { functions->SetByteArrayRegion(this, array, start, len, buf); } + void SetCharArrayRegion(jcharArray array, jsize start, jsize len, + const jchar* buf) + { functions->SetCharArrayRegion(this, array, start, len, buf); } + void SetShortArrayRegion(jshortArray array, jsize start, jsize len, + const jshort* buf) + { functions->SetShortArrayRegion(this, array, start, len, buf); } + void SetIntArrayRegion(jintArray array, jsize start, jsize len, + const jint* buf) + { functions->SetIntArrayRegion(this, array, start, len, buf); } + void SetLongArrayRegion(jlongArray array, jsize start, jsize len, + const jlong* buf) + { functions->SetLongArrayRegion(this, array, start, len, buf); } + void SetFloatArrayRegion(jfloatArray array, jsize start, jsize len, + const jfloat* buf) + { functions->SetFloatArrayRegion(this, array, start, len, buf); } + void SetDoubleArrayRegion(jdoubleArray array, jsize start, jsize len, + const jdouble* buf) + { functions->SetDoubleArrayRegion(this, array, start, len, buf); } + + jint RegisterNatives(jclass clazz, const JNINativeMethod* methods, + jint nMethods) + { return functions->RegisterNatives(this, clazz, methods, nMethods); } + + jint UnregisterNatives(jclass clazz) + { return functions->UnregisterNatives(this, clazz); } + + jint MonitorEnter(jobject obj) + { return functions->MonitorEnter(this, obj); } + + jint MonitorExit(jobject obj) + { return functions->MonitorExit(this, obj); } + + jint GetJavaVM(JavaVM** vm) + { return functions->GetJavaVM(this, vm); } + + void GetStringRegion(jstring str, jsize start, jsize len, jchar* buf) + { functions->GetStringRegion(this, str, start, len, buf); } + + void GetStringUTFRegion(jstring str, jsize start, jsize len, char* buf) + { return functions->GetStringUTFRegion(this, str, start, len, buf); } + + void* GetPrimitiveArrayCritical(jarray array, jboolean* isCopy) + { return functions->GetPrimitiveArrayCritical(this, array, isCopy); } + + void ReleasePrimitiveArrayCritical(jarray array, void* carray, jint mode) + { functions->ReleasePrimitiveArrayCritical(this, array, carray, mode); } + + const jchar* GetStringCritical(jstring string, jboolean* isCopy) + { return functions->GetStringCritical(this, string, isCopy); } + + void ReleaseStringCritical(jstring string, const jchar* carray) + { functions->ReleaseStringCritical(this, string, carray); } + + jweak NewWeakGlobalRef(jobject obj) + { return functions->NewWeakGlobalRef(this, obj); } + + void DeleteWeakGlobalRef(jweak obj) + { functions->DeleteWeakGlobalRef(this, obj); } + + jboolean ExceptionCheck() + { return functions->ExceptionCheck(this); } + + jobject NewDirectByteBuffer(void* address, jlong capacity) + { return functions->NewDirectByteBuffer(this, address, capacity); } + + void* GetDirectBufferAddress(jobject buf) + { return functions->GetDirectBufferAddress(this, buf); } + + jlong GetDirectBufferCapacity(jobject buf) + { return functions->GetDirectBufferCapacity(this, buf); } + + /* added in JNI 1.6 */ + jobjectRefType GetObjectRefType(jobject obj) + { return functions->GetObjectRefType(this, obj); } +#endif /*__cplusplus*/ +}; + + +/* + * JNI invocation interface. + */ +struct JNIInvokeInterface { + void* reserved0; + void* reserved1; + void* reserved2; + + jint (*DestroyJavaVM)(JavaVM*); + jint (*AttachCurrentThread)(JavaVM*, JNIEnv**, void*); + jint (*DetachCurrentThread)(JavaVM*); + jint (*GetEnv)(JavaVM*, void**, jint); + jint (*AttachCurrentThreadAsDaemon)(JavaVM*, JNIEnv**, void*); +}; + +/* + * C++ version. + */ +struct _JavaVM { + const struct JNIInvokeInterface* functions; + +#if defined(__cplusplus) + jint DestroyJavaVM() + { return functions->DestroyJavaVM(this); } + jint AttachCurrentThread(JNIEnv** p_env, void* thr_args) + { return functions->AttachCurrentThread(this, p_env, thr_args); } + jint DetachCurrentThread() + { return functions->DetachCurrentThread(this); } + jint GetEnv(void** env, jint version) + { return functions->GetEnv(this, env, version); } + jint AttachCurrentThreadAsDaemon(JNIEnv** p_env, void* thr_args) + { return functions->AttachCurrentThreadAsDaemon(this, p_env, thr_args); } +#endif /*__cplusplus*/ +}; + +struct JavaVMAttachArgs { + jint version; /* must be >= JNI_VERSION_1_2 */ + const char* name; /* NULL or name of thread as modified UTF-8 str */ + jobject group; /* global ref of a ThreadGroup object, or NULL */ +}; +typedef struct JavaVMAttachArgs JavaVMAttachArgs; + +/* + * JNI 1.2+ initialization. (As of 1.6, the pre-1.2 structures are no + * longer supported.) + */ +typedef struct JavaVMOption { + const char* optionString; + void* extraInfo; +} JavaVMOption; + +typedef struct JavaVMInitArgs { + jint version; /* use JNI_VERSION_1_2 or later */ + + jint nOptions; + JavaVMOption* options; + jboolean ignoreUnrecognized; +} JavaVMInitArgs; + +#ifdef __cplusplus +extern "C" { +#endif +/* + * VM initialization functions. + * + * Note these are the only symbols exported for JNI by the VM. + */ +jint JNI_GetDefaultJavaVMInitArgs(void*); +jint JNI_CreateJavaVM(JavaVM**, JNIEnv**, void*); +jint JNI_GetCreatedJavaVMs(JavaVM**, jsize, jsize*); + +/* + * Prototypes for functions exported by loadable shared libs. These are + * called by JNI, not provided by JNI. + */ +jint JNI_OnLoad(JavaVM* vm, void* reserved); +void JNI_OnUnload(JavaVM* vm, void* reserved); + +#ifdef __cplusplus +} +#endif + + +/* + * Manifest constants. + */ +#define JNI_FALSE 0 +#define JNI_TRUE 1 + +#define JNI_VERSION_1_1 0x00010001 +#define JNI_VERSION_1_2 0x00010002 +#define JNI_VERSION_1_4 0x00010004 +#define JNI_VERSION_1_6 0x00010006 + +#define JNI_OK (0) /* no error */ +#define JNI_ERR (-1) /* generic error */ +#define JNI_EDETACHED (-2) /* thread detached from the VM */ +#define JNI_EVERSION (-3) /* JNI version error */ + +#define JNI_COMMIT 1 /* copy content, do not free buffer */ +#define JNI_ABORT 2 /* free buffer w/o copying back */ + +/* need these for Windows-aware headers */ +#define JNIIMPORT +#define JNIEXPORT +#define JNICALL + +#endif /*_JNI_H*/ diff --git a/tools/autodebugger_c.py b/tools/autodebugger_c.py index 5d41faf0..54a4d691 100644 --- a/tools/autodebugger_c.py +++ b/tools/autodebugger_c.py @@ -21,9 +21,13 @@ for filename in filenames: if m and (' if ' not in lines[i-1] or '{' in lines[i-1]) and \ (' if ' not in lines[i+1] or '{' in lines[i+1]) and \ (' else' not in lines[i-1] or '{' in lines[i-1]) and \ - (' else' not in lines[i+1] or '{' in lines[i+1]): - var = m.groups(1)[0].rstrip().split(' ')[-1] - lines[i] += ''' printf("%s:%d:%s=%%d\\n", %s);''' % (filename, i+1, var, var) + (' else' not in lines[i+1] or '{' in lines[i+1]) and \ + (' for' not in lines[i-1]) and \ + ('struct' not in lines[i]): + raw = m.groups(1)[0].rstrip() + var = raw.split(' ')[-1] + if ' ' in raw and '[' in var: continue + lines[i] += ''' printf("%s:%d:%s=%%d\\n", (int)%s);''' % (filename, i+1, var, var) f = open(filename, 'w') f.write('\n'.join(lines)) diff --git a/tools/cache.py b/tools/cache.py new file mode 100644 index 00000000..13a53fe9 --- /dev/null +++ b/tools/cache.py @@ -0,0 +1,195 @@ +import os.path, sys, shutil, hashlib, cPickle, zlib, time + +import tempfiles + +# Permanent cache for dlmalloc and stdlibc++ +class Cache: + def __init__(self, dirname=None, debug=False): + if dirname is None: + dirname = os.environ.get('EM_CACHE') + if not dirname: + dirname = os.path.expanduser(os.path.join('~', '.emscripten_cache')) + self.dirname = dirname + self.debug = debug + + def ensure(self): + if not os.path.exists(self.dirname): + os.makedirs(self.dirname) + + def erase(self): + tempfiles.try_delete(self.dirname) + try: + open(self.dirname + '__last_clear', 'w').write('last clear: ' + time.asctime() + '\n') + except Exception, e: + print >> sys.stderr, 'failed to save last clear time: ', e + + def get_path(self, shortname): + return os.path.join(self.dirname, shortname) + + # Request a cached file. If it isn't in the cache, it will be created with + # the given creator function + def get(self, shortname, creator, extension='.bc'): + if not shortname.endswith(extension): shortname += extension + cachename = os.path.join(self.dirname, shortname) + if os.path.exists(cachename): + return cachename + self.ensure() + shutil.copyfile(creator(), cachename) + return cachename + +# JS-specific cache. We cache the results of compilation and optimization, +# so that in incremental builds we can just load from cache. +# We cache reasonably-large-sized chunks +class JCache: + def __init__(self, cache): + self.cache = cache + self.dirname = os.path.join(cache.dirname, 'jcache') + self.debug = cache.debug + + def ensure(self): + self.cache.ensure() + if not os.path.exists(self.dirname): + os.makedirs(self.dirname) + + def get_shortkey(self, keys): + if type(keys) not in [list, tuple]: + keys = [keys] + ret = '' + for key in keys: + assert type(key) == str + ret += hashlib.md5(key).hexdigest() + return ret + + def get_cachename(self, shortkey): + return os.path.join(self.dirname, shortkey) + + # Returns a cached value, if it exists. Make sure the full key matches + def get(self, shortkey, keys): + if self.debug: print >> sys.stderr, 'jcache get?', shortkey + cachename = self.get_cachename(shortkey) + if not os.path.exists(cachename): + if self.debug: print >> sys.stderr, 'jcache none at all' + return + try: + data = cPickle.loads(zlib.decompress(open(cachename).read())) + except Exception, e: + if self.debug: print >> sys.stderr, 'jcache decompress/unpickle error:', e + return + if len(data) != 2: + if self.debug: print >> sys.stderr, 'jcache error in get' + return + oldkeys = data[0] + if len(oldkeys) != len(keys): + if self.debug: print >> sys.stderr, 'jcache collision (a)' + return + for i in range(len(oldkeys)): + if oldkeys[i] != keys[i]: + if self.debug: print >> sys.stderr, 'jcache collision (b)' + return + if self.debug: print >> sys.stderr, 'jcache win' + return data[1] + + # Sets the cached value for a key (from get_key) + def set(self, shortkey, keys, value): + cachename = self.get_cachename(shortkey) + try: + f = open(cachename, 'w') + f.write(zlib.compress(cPickle.dumps([keys, value]))) + f.close() + except Exception, e: + if self.debug: print >> sys.stderr, 'jcache compress/pickle error:', e + return + # for i in range(len(keys)): + # open(cachename + '.key' + str(i), 'w').write(keys[i]) + # open(cachename + '.value', 'w').write(value) + +# Given a set of functions of form (ident, text), and a preferred chunk size, +# generates a set of chunks for parallel processing and caching. +# It is very important to generate similar chunks in incremental builds, in +# order to maximize the chance of cache hits. To achieve that, we save the +# chunking used in the previous compilation of this phase, and we try to +# generate the same chunks, barring big differences in function sizes that +# violate our chunk size guideline. If caching is not used, chunking_file +# should be None +def chunkify(funcs, chunk_size, chunking_file, DEBUG=False): + previous_mapping = None + if chunking_file: + chunking_file = chunking_file + if os.path.exists(chunking_file): + try: + previous_mapping = cPickle.Unpickler(open(chunking_file, 'rb')).load() # maps a function identifier to the chunk number it will be in + #if DEBUG: print >> sys.stderr, 'jscache previous mapping', previous_mapping + except: + pass + chunks = [] + if previous_mapping: + # initialize with previous chunking + news = [] + for func in funcs: + ident, data = func + assert ident, 'need names for jcache chunking' + if not ident in previous_mapping: + news.append(func) + else: + n = previous_mapping[ident] + while n >= len(chunks): chunks.append([]) + chunks[n].append(func) + if DEBUG: print >> sys.stderr, 'jscache not in previous chunking', len(news) + # add news and adjust for new sizes + spilled = news + for i in range(len(chunks)): + chunk = chunks[i] + size = sum([len(func[1]) for func in chunk]) + #if DEBUG: print >> sys.stderr, 'need spilling?', i, size, len(chunk), 'vs', chunk_size, 1.5*chunk_size + while size > 1.5*chunk_size and len(chunk) > 1: + spill = chunk.pop() + spilled.append(spill) + size -= len(spill[1]) + #if DEBUG: print >> sys.stderr, 'jscache new + spilled', len(spilled) + for chunk in chunks: + size = sum([len(func[1]) for func in chunk]) + while size < 0.66*chunk_size and len(spilled) > 0: + spill = spilled.pop() + chunk.append(spill) + size += len(spill[1]) + chunks = filter(lambda chunk: len(chunk) > 0, chunks) # might have empty ones, eliminate them + funcs = spilled # we will allocate these into chunks as if they were normal inputs + #if DEBUG: print >> sys.stderr, 'leftover spills', len(spilled) + # initialize reasonably, the rest of the funcs we need to split out + curr = [] + total_size = 0 + for i in range(len(funcs)): + func = funcs[i] + curr_size = len(func[1]) + if total_size + curr_size < chunk_size: + curr.append(func) + total_size += curr_size + else: + chunks.append(curr) + curr = [func] + total_size = curr_size + if curr: + chunks.append(curr) + curr = None + if chunking_file: + # sort within each chunk, to keep the order identical + for chunk in chunks: + chunk.sort(key=lambda func: func[0]) + # save new mapping info + new_mapping = {} + for i in range(len(chunks)): + chunk = chunks[i] + for ident, data in chunk: + assert ident not in new_mapping, 'cannot have duplicate names in jcache chunking' + new_mapping[ident] = i + cPickle.Pickler(open(chunking_file, 'wb')).dump(new_mapping) + #if DEBUG: + # for i in range(len(chunks)): + # chunk = chunks[i] + # print >> sys.stderr, 'final chunk', i, len(chunk) + # print >> sys.stderr, 'new mapping:', new_mapping + # if previous_mapping: + # for ident in set(previous_mapping.keys() + new_mapping.keys()): + # if previous_mapping.get(ident) != new_mapping.get(ident): + # print >> sys.stderr, 'mapping inconsistency', ident, previous_mapping.get(ident), new_mapping.get(ident) + return [''.join([func[1] for func in chunk]) for chunk in chunks] # remove function names diff --git a/tools/eliminator/node_modules/.bin/cake b/tools/eliminator/node_modules/.bin/cake deleted file mode 120000 index d95f32af..00000000 --- a/tools/eliminator/node_modules/.bin/cake +++ /dev/null @@ -1 +0,0 @@ -../coffee-script/bin/cake
\ No newline at end of file diff --git a/tools/eliminator/node_modules/.bin/coffee b/tools/eliminator/node_modules/.bin/coffee deleted file mode 120000 index b57f275d..00000000 --- a/tools/eliminator/node_modules/.bin/coffee +++ /dev/null @@ -1 +0,0 @@ -../coffee-script/bin/coffee
\ No newline at end of file diff --git a/tools/file_packager.py b/tools/file_packager.py index 7e196efd..73ff4919 100644 --- a/tools/file_packager.py +++ b/tools/file_packager.py @@ -35,8 +35,8 @@ TODO: You can also provide .crn files yourself, pre-crunched. With this o import os, sys, shutil, random -from shared import Compression, execute, suffix, unsuffixed import shared +from shared import Compression, execute, suffix, unsuffixed from subprocess import Popen, PIPE, STDOUT data_target = sys.argv[1] @@ -228,7 +228,7 @@ if has_preloaded: curr = open(file_['localname'], 'rb').read() file_['data_end'] = start + len(curr) if AV_WORKAROUND: curr += '\x00' - print >> sys.stderr, 'bundling', file_['name'], file_['localname'], file_['data_start'], file_['data_end'] + #print >> sys.stderr, 'bundling', file_['name'], file_['localname'], file_['data_start'], file_['data_end'] start += len(curr) data.write(curr) data.close() diff --git a/tools/js-optimizer.js b/tools/js-optimizer.js index efbfa8aa..f2dc516a 100644 --- a/tools/js-optimizer.js +++ b/tools/js-optimizer.js @@ -1598,7 +1598,7 @@ function registerize(ast) { fun[2].push(reg); } } - getStatements(fun).unshift(['var', vars]); + if (vars.length > 0) getStatements(fun).unshift(['var', vars]); } } else { //printErr('unfake params: \n\n' + astToSrc(fun) + '\n\n'); diff --git a/tools/js_optimizer.py b/tools/js_optimizer.py index f2e610d0..2fd2211b 100644 --- a/tools/js_optimizer.py +++ b/tools/js_optimizer.py @@ -2,7 +2,8 @@ import os, sys, subprocess, multiprocessing, re import shared -temp_files = shared.TempFiles() +configuration = shared.configuration +temp_files = configuration.get_temp_files() __rootpath__ = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) def path_from_root(*pathelems): @@ -10,7 +11,9 @@ def path_from_root(*pathelems): JS_OPTIMIZER = path_from_root('tools', 'js-optimizer.js') -BEST_JS_PROCESS_SIZE = 1024*1024 +NUM_CHUNKS_PER_CORE = 1.5 +MIN_CHUNK_SIZE = int(os.environ.get('EMCC_JSOPT_MIN_CHUNK_SIZE') or 1024*1024) # configuring this is just for debugging purposes +MAX_CHUNK_SIZE = 20*1024*1024 WINDOWS = sys.platform.startswith('win') @@ -28,7 +31,7 @@ def run_on_chunk(command): return filename def run_on_js(filename, passes, js_engine, jcache): - + if isinstance(jcache, bool) and jcache: jcache = shared.JCache if jcache: shared.JCache.ensure() if type(passes) == str: @@ -74,6 +77,8 @@ def run_on_js(filename, passes, js_engine, jcache): assert gen_end > gen_start pre = js[:gen_start] post = js[gen_end:] + if 'last' in passes: + post = post.replace(suffix, '') # no need to write out the metadata - nothing after us needs it js = js[gen_start:gen_end] else: pre = '' @@ -88,16 +93,21 @@ def run_on_js(filename, passes, js_engine, jcache): if i < len(parts)-1: func += '\n}\n' # last part needs no } m = func_sig.search(func) if m: - ident = m.group(1) + ident = m.group(2) else: if suffix: continue # ignore whitespace ident = 'anon_%d' % i + assert ident funcs.append((ident, func)) parts = None total_size = len(js) js = None - chunks = shared.JCache.chunkify(funcs, BEST_JS_PROCESS_SIZE, 'jsopt' if jcache else None) + cores = int(os.environ.get('EMCC_CORES') or multiprocessing.cpu_count()) + intended_num_chunks = int(round(cores * NUM_CHUNKS_PER_CORE)) + chunk_size = min(MAX_CHUNK_SIZE, max(MIN_CHUNK_SIZE, total_size / intended_num_chunks)) + + chunks = shared.chunkify(funcs, chunk_size, jcache.get_cachename('jsopt') if jcache else None) if jcache: # load chunks from cache where we can # TODO: ignore small chunks @@ -130,18 +140,18 @@ def run_on_js(filename, passes, js_engine, jcache): if len(filenames) > 0: # XXX Use '--nocrankshaft' to disable crankshaft to work around v8 bug 1895, needed for older v8/node (node 0.6.8+ should be ok) - commands = map(lambda filename: [js_engine, JS_OPTIMIZER, filename, 'noPrintMetadata'] + passes, filenames) + commands = map(lambda filename: js_engine + [JS_OPTIMIZER, filename, 'noPrintMetadata'] + passes, filenames) #print [' '.join(command) for command in commands] - cores = min(multiprocessing.cpu_count(), filenames) + cores = min(cores, filenames) if len(chunks) > 1 and cores >= 2: # We can parallelize - if DEBUG: print >> sys.stderr, 'splitting up js optimization into %d chunks, using %d cores (total: %.2f MB)' % (len(chunks), cores, total_size/(1024*1024.)) + if DEBUG: print >> sys.stderr, 'splitting up js optimization into %d chunks of size %d, using %d cores (total: %.2f MB)' % (len(chunks), chunk_size, cores, total_size/(1024*1024.)) pool = multiprocessing.Pool(processes=cores) filenames = pool.map(run_on_chunk, commands, chunksize=1) else: # We can't parallize, but still break into chunks to avoid uglify/node memory issues - if len(chunks) > 1 and DEBUG: print >> sys.stderr, 'splitting up js optimization into %d chunks' % (len(chunks)) + if len(chunks) > 1 and DEBUG: print >> sys.stderr, 'splitting up js optimization into %d chunks of size %d' % (len(chunks), chunk_size) filenames = [run_on_chunk(command) for command in commands] else: filenames = [] diff --git a/tools/jsrun.py b/tools/jsrun.py new file mode 100644 index 00000000..27c55350 --- /dev/null +++ b/tools/jsrun.py @@ -0,0 +1,27 @@ +import time +from subprocess import Popen, PIPE, STDOUT + +def timeout_run(proc, timeout, note='unnamed process', full_output=False): + start = time.time() + if timeout is not None: + while time.time() - start < timeout and proc.poll() is None: + time.sleep(0.1) + if proc.poll() is None: + proc.kill() # XXX bug: killing emscripten.py does not kill it's child process! + raise Exception("Timed out: " + note) + out = proc.communicate() + return '\n'.join(out) if full_output else out[0] + +def run_js(filename, engine=None, args=[], check_timeout=False, stdout=PIPE, stderr=None, cwd=None, full_output=False): + if type(engine) is not list: + engine = [engine] + command = engine + [filename] + (['--'] if 'd8' in engine[0] else []) + args + return timeout_run( + Popen( + command, + stdout=stdout, + stderr=stderr, + cwd=cwd), + 15*60 if check_timeout else None, + 'Execution', + full_output=full_output) diff --git a/tools/shared.py b/tools/shared.py index c283c1f5..e60800a5 100644 --- a/tools/shared.py +++ b/tools/shared.py @@ -1,6 +1,11 @@ -import shutil, time, os, sys, json, tempfile, copy, shlex, atexit, subprocess, hashlib, cPickle +import shutil, time, os, sys, json, tempfile, copy, shlex, atexit, subprocess, hashlib, cPickle, re from subprocess import Popen, PIPE, STDOUT from tempfile import mkstemp +import jsrun, cache, tempfiles + +def listify(x): + if type(x) is not list: return [x] + return x # On Windows python suffers from a particularly nasty bug if python is spawning new processes while python itself is spawned from some other non-console process. # Use a custom replacement for Popen on Windows to avoid the "WindowsError: [Error 6] The handle is invalid" errors when emcc is driven through cmake or mingw32-make. @@ -28,7 +33,10 @@ class WindowsPopen: self.stderr_ = PIPE # Call the process with fixed streams. - self.process = subprocess.Popen(args, bufsize, executable, self.stdin_, self.stdout_, self.stderr_, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags) + try: + self.process = subprocess.Popen(args, bufsize, executable, self.stdin_, self.stdout_, self.stderr_, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags) + except Exception, e: + print >> sys.stderr, 'subprocess.Popen(args=%s) failed! Exception %s' % (' '.join(args), str(e)) def communicate(self, input=None): output = self.process.communicate(input) @@ -155,7 +163,8 @@ EXPECTED_NODE_VERSION = (0,6,8) def check_node_version(): try: - actual = Popen([NODE_JS, '--version'], stdout=PIPE).communicate()[0].strip() + node = listify(NODE_JS) + actual = Popen(node + ['--version'], stdout=PIPE).communicate()[0].strip() version = tuple(map(int, actual.replace('v', '').split('.'))) if version >= EXPECTED_NODE_VERSION: return True @@ -172,7 +181,7 @@ def check_node_version(): # we re-check sanity when the settings are changed) # We also re-check sanity and clear the cache when the version changes -EMSCRIPTEN_VERSION = '1.2.4' +EMSCRIPTEN_VERSION = '1.2.9' def check_sanity(force=False): try: @@ -281,30 +290,50 @@ AUTODEBUGGER = path_from_root('tools', 'autodebugger.py') BINDINGS_GENERATOR = path_from_root('tools', 'bindings_generator.py') EXEC_LLVM = path_from_root('tools', 'exec_llvm.py') FILE_PACKAGER = path_from_root('tools', 'file_packager.py') -RELOOPER = path_from_root('src', 'relooper.js') # Temp dir. Create a random one, unless EMCC_DEBUG is set, in which case use TEMP_DIR/emscripten_temp -try: - TEMP_DIR -except: - print >> sys.stderr, 'TEMP_DIR not defined in ~/.emscripten, using /tmp' - TEMP_DIR = '/tmp' +class Configuration: + def __init__(self, environ): + self.DEBUG = environ.get('EMCC_DEBUG') + if self.DEBUG == "0": + self.DEBUG = None + self.DEBUG_CACHE = self.DEBUG and "cache" in self.DEBUG + self.EMSCRIPTEN_TEMP_DIR = None -CANONICAL_TEMP_DIR = os.path.join(TEMP_DIR, 'emscripten_temp') -EMSCRIPTEN_TEMP_DIR = None + try: + self.TEMP_DIR = TEMP_DIR + except NameError: + print >> sys.stderr, 'TEMP_DIR not defined in ~/.emscripten, using /tmp' + self.TEMP_DIR = '/tmp' -DEBUG = os.environ.get('EMCC_DEBUG') -if DEBUG: - try: - EMSCRIPTEN_TEMP_DIR = CANONICAL_TEMP_DIR - if not os.path.exists(EMSCRIPTEN_TEMP_DIR): - os.makedirs(EMSCRIPTEN_TEMP_DIR) - except Exception, e: - print >> sys.stderr, e, 'Could not create canonical temp dir. Check definition of TEMP_DIR in ~/.emscripten' + self.CANONICAL_TEMP_DIR = os.path.join(self.TEMP_DIR, 'emscripten_temp') + + if self.DEBUG: + try: + self.EMSCRIPTEN_TEMP_DIR = self.CANONICAL_TEMP_DIR + if not os.path.exists(self.EMSCRIPTEN_TEMP_DIR): + os.makedirs(self.EMSCRIPTEN_TEMP_DIR) + except Exception, e: + print >> sys.stderr, e, 'Could not create canonical temp dir. Check definition of TEMP_DIR in ~/.emscripten' + + def get_temp_files(self): + return tempfiles.TempFiles( + tmp=self.TEMP_DIR if not self.DEBUG else self.EMSCRIPTEN_TEMP_DIR, + save_debug_files=os.environ.get('EMCC_DEBUG_SAVE')) + + def debug_log(self, msg): + if self.DEBUG: + print >> sys.stderr, msg + +configuration = Configuration(environ=os.environ) +DEBUG = configuration.DEBUG +EMSCRIPTEN_TEMP_DIR = configuration.EMSCRIPTEN_TEMP_DIR +DEBUG_CACHE = configuration.DEBUG_CACHE +CANONICAL_TEMP_DIR = configuration.CANONICAL_TEMP_DIR if not EMSCRIPTEN_TEMP_DIR: - EMSCRIPTEN_TEMP_DIR = tempfile.mkdtemp(prefix='emscripten_temp_', dir=TEMP_DIR) + EMSCRIPTEN_TEMP_DIR = tempfile.mkdtemp(prefix='emscripten_temp_', dir=configuration.TEMP_DIR) def clean_temp(): try_delete(EMSCRIPTEN_TEMP_DIR) atexit.register(clean_temp) @@ -359,11 +388,11 @@ if USE_EMSDK: # Note that -nostdinc++ is not needed, since -nostdinc implies that! EMSDK_OPTS = ['-nostdinc', '-Xclang', '-nobuiltininc', '-Xclang', '-nostdsysteminc', '-Xclang', '-isystem' + path_from_root('system', 'local', 'include'), + '-Xclang', '-isystem' + path_from_root('system', 'include', 'libcxx'), '-Xclang', '-isystem' + path_from_root('system', 'include'), '-Xclang', '-isystem' + path_from_root('system', 'include', 'emscripten'), '-Xclang', '-isystem' + path_from_root('system', 'include', 'bsd'), # posix stuff '-Xclang', '-isystem' + path_from_root('system', 'include', 'libc'), - '-Xclang', '-isystem' + path_from_root('system', 'include', 'libcxx'), '-Xclang', '-isystem' + path_from_root('system', 'lib', 'libcxxabi', 'include'), '-Xclang', '-isystem' + path_from_root('system', 'include', 'gfx'), '-Xclang', '-isystem' + path_from_root('system', 'include', 'net'), @@ -376,6 +405,9 @@ if USE_EMSDK: else: EMSDK_OPTS = [] +#print >> sys.stderr, 'SDK opts', ' '.join(EMSDK_OPTS) +#print >> sys.stderr, 'Compiler opts', ' '.join(COMPILER_OPTS) + # Engine tweaks try: @@ -399,42 +431,7 @@ if not WINDOWS: pass # Temp file utilities - -def try_delete(filename): - try: - os.unlink(filename) - except: - try: - shutil.rmtree(filename) - except: - pass - -class TempFiles: - def __init__(self): - self.to_clean = [] - - def note(self, filename): - self.to_clean.append(filename) - - def get(self, suffix): - """Returns a named temp file with the given prefix.""" - named_file = tempfile.NamedTemporaryFile(dir=TEMP_DIR if not DEBUG else EMSCRIPTEN_TEMP_DIR, suffix=suffix, delete=False) - self.note(named_file.name) - return named_file - - def clean(self): - if os.environ.get('EMCC_DEBUG_SAVE'): - print >> sys.stderr, 'not cleaning up temp files since in debug-save mode, see them in %s' % EMSCRIPTEN_TEMP_DIR - return - for filename in self.to_clean: - try_delete(filename) - self.to_clean = [] - - def run_and_clean(self, func): - try: - return func() - finally: - self.clean() +from tempfiles import try_delete # Utilities @@ -448,22 +445,10 @@ def check_engine(engine): print 'Checking JS engine %s failed. Check %s. Details: %s' % (str(engine), EM_CONFIG, str(e)) return False -def timeout_run(proc, timeout, note='unnamed process', full_output=False): - start = time.time() - if timeout is not None: - while time.time() - start < timeout and proc.poll() is None: - time.sleep(0.1) - if proc.poll() is None: - proc.kill() # XXX bug: killing emscripten.py does not kill it's child process! - raise Exception("Timed out: " + note) - out = proc.communicate() - return '\n'.join(out) if full_output else out[0] - -def run_js(filename, engine=None, args=[], check_timeout=False, stdout=PIPE, stderr=None, cwd=None, full_output=False): - if engine is None: engine = JS_ENGINES[0] - if type(engine) is not list: engine = [engine] - command = engine + [filename] + (['--'] if 'd8' in engine[0] else []) + args - return timeout_run(Popen(command, stdout=stdout, stderr=stderr, cwd=cwd), 15*60 if check_timeout else None, 'Execution', full_output=full_output) +def run_js(filename, engine=None, *args, **kw): + if engine is None: + engine = JS_ENGINES[0] + return jsrun.run_js(filename, engine, *args, **kw) def to_cc(cxx): # By default, LLVM_GCC and CLANG are really the C++ versions. This gets an explicit C version @@ -554,7 +539,7 @@ class Settings: ret = [] for key, value in Settings.__dict__.iteritems(): if key == key.upper(): # this is a hack. all of our settings are ALL_CAPS, python internals are not - jsoned = json.dumps(value) + jsoned = json.dumps(value, sort_keys=True) ret += ['-s', key + '=' + jsoned] return ret @@ -563,15 +548,13 @@ class Settings: if opt_level >= 1: Settings.ASSERTIONS = 0 Settings.DISABLE_EXCEPTION_CATCHING = 1 + Settings.EMIT_GENERATED_FUNCTIONS = 1 if opt_level >= 2: Settings.RELOOP = 1 if opt_level >= 3: - Settings.INLINING_LIMIT = 0 + # Aside from these, -O3 also runs closure compiler and llvm lto Settings.DOUBLE_MODE = 0 Settings.PRECISE_I64_MATH = 0 - Settings.CORRECT_SIGNS = 0 - Settings.CORRECT_OVERFLOWS = 0 - Settings.CORRECT_ROUNDINGS = 0 if noisy: print >> sys.stderr, 'Warning: Applying some potentially unsafe optimizations! (Use -O2 if this fails.)' global Settings @@ -640,7 +623,7 @@ set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)''' % { 'winfix': '' if not WINDOWS e .replace('$EMSCRIPTEN_ROOT', path_from_root('').replace('\\', '/')) \ .replace('$CFLAGS', env['CFLAGS']) \ .replace('$CXXFLAGS', env['CFLAGS']) - toolchainFile = mkstemp(suffix='.cmaketoolchain.txt', dir=TEMP_DIR)[1] + toolchainFile = mkstemp(suffix='.cmaketoolchain.txt', dir=configuration.TEMP_DIR)[1] open(toolchainFile, 'w').write(CMakeToolchain) args.append('-DCMAKE_TOOLCHAIN_FILE=%s' % os.path.abspath(toolchainFile)) return args @@ -665,6 +648,9 @@ set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)''' % { 'winfix': '' if not WINDOWS e def make(args, stdout=None, stderr=None, env=None): if env is None: env = Building.get_building_env() + if not args: + print >> sys.stderr, 'Error: Executable to run not specified.' + sys.exit(1) #args += ['VERBOSE=1'] try: Popen(args, stdout=stdout, stderr=stderr, env=env).communicate() @@ -748,12 +734,16 @@ set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)''' % { 'winfix': '' if not WINDOWS e resolved_symbols = set() temp_dirs = [] files = map(os.path.abspath, files) + has_ar = False + for f in files: + has_ar = has_ar or Building.is_ar(f) for f in files: if not Building.is_ar(f): if Building.is_bitcode(f): - new_symbols = Building.llvm_nm(f) - resolved_symbols = resolved_symbols.union(new_symbols.defs) - unresolved_symbols = unresolved_symbols.union(new_symbols.undefs.difference(resolved_symbols)).difference(new_symbols.defs) + if has_ar: + new_symbols = Building.llvm_nm(f) + resolved_symbols = resolved_symbols.union(new_symbols.defs) + unresolved_symbols = unresolved_symbols.union(new_symbols.undefs.difference(resolved_symbols)).difference(new_symbols.defs) actual_files.append(f) else: # Extract object files from ar archives, and link according to gnu ld semantics @@ -806,7 +796,37 @@ set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)''' % { 'winfix': '' if not WINDOWS e # Finish link actual_files = unique_ordered(actual_files) # tolerate people trying to link a.so a.so etc. if DEBUG: print >>sys.stderr, 'emcc: llvm-linking:', actual_files - output = Popen([LLVM_LINK] + actual_files + ['-o', target], stdout=PIPE).communicate()[0] + + # check for too-long command line + link_cmd = [LLVM_LINK] + actual_files + ['-o', target] + # 8k is a bit of an arbitrary limit, but a reasonable one + # for max command line size before we use a respose file + response_file = None + if WINDOWS and len(' '.join(link_cmd)) > 8192: + if DEBUG: print >>sys.stderr, 'using response file for llvm-link' + [response_fd, response_file] = mkstemp(suffix='.response', dir=TEMP_DIR) + + link_cmd = [LLVM_LINK, "@" + response_file] + + response_fh = os.fdopen(response_fd, 'w') + for arg in actual_files: + # we can't put things with spaces in the response file + if " " in arg: + link_cmd.append(arg) + else: + response_fh.write(arg + "\n") + response_fh.close() + link_cmd.append("-o") + link_cmd.append(target) + + if len(' '.join(link_cmd)) > 8192: + print >>sys.stderr, 'emcc: warning: link command line is very long, even with response file -- use paths with no spaces' + + output = Popen(link_cmd, stdout=PIPE).communicate()[0] + + if response_file: + os.unlink(response_file) + assert os.path.exists(target) and (output is None or 'Could not open input file' not in output), 'Linking error: ' + output for temp_dir in temp_dirs: try_delete(temp_dir) @@ -828,6 +848,7 @@ set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)''' % { 'winfix': '' if not WINDOWS e def llvm_opt(filename, opts): if type(opts) is int: opts = Building.pick_llvm_opts(opts) + #opts += ['-debug-pass=Arguments'] if DEBUG: print >> sys.stderr, 'emcc: LLVM opts:', opts output = Popen([LLVM_OPT, filename] + opts + ['-o=' + filename + '.opt.bc'], stdout=PIPE).communicate()[0] assert os.path.exists(filename + '.opt.bc'), 'Failed to run llvm optimizations: ' + output @@ -864,8 +885,14 @@ set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)''' % { 'winfix': '' if not WINDOWS e assert os.path.exists(output_filename), 'Could not create bc file: ' + output return output_filename + nm_cache = {} # cache results of nm - it can be slow to run + @staticmethod def llvm_nm(filename, stdout=PIPE, stderr=None): + if filename in Building.nm_cache: + #if DEBUG: print >> sys.stderr, 'loading nm results for %s from cache' % filename + return Building.nm_cache[filename] + # LLVM binary ==> list of symbols output = Popen([LLVM_NM, filename], stdout=stdout, stderr=stderr).communicate()[0] class ret: @@ -886,6 +913,7 @@ set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)''' % { 'winfix': '' if not WINDOWS e ret.defs = set(ret.defs) ret.undefs = set(ret.undefs) ret.commons = set(ret.commons) + Building.nm_cache[filename] = ret return ret @staticmethod @@ -894,14 +922,14 @@ set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)''' % { 'winfix': '' if not WINDOWS e output_filename = filename + '.o' try_delete(output_filename) Popen([PYTHON, EMCC, filename] + args + ['-o', output_filename], stdout=stdout, stderr=stderr, env=env).communicate() - assert os.path.exists(output_filename), 'emcc could not create output file' + assert os.path.exists(output_filename), 'emcc could not create output file: ' + output_filename @staticmethod def emar(action, output_filename, filenames, stdout=None, stderr=None, env=None): try_delete(output_filename) Popen([PYTHON, EMAR, action, output_filename] + filenames, stdout=stdout, stderr=stderr, env=env).communicate() if 'c' in action: - assert os.path.exists(output_filename), 'emar could not create output file' + assert os.path.exists(output_filename), 'emar could not create output file: ' + output_filename @staticmethod def emscripten(filename, append_ext=True, extra_args=[]): @@ -909,8 +937,9 @@ set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)''' % { 'winfix': '' if not WINDOWS e os.environ['EMSCRIPTEN_SUPPRESS_USAGE_WARNING'] = '1' # Run Emscripten + Settings.RELOOPER = Cache.get_path('relooper.js') settings = Settings.serialize() - compiler_output = timeout_run(Popen([PYTHON, EMSCRIPTEN, filename + ('.o.ll' if append_ext else ''), '-o', filename + '.o.js'] + settings + extra_args, stdout=PIPE), None, 'Compiling') + compiler_output = jsrun.timeout_run(Popen([PYTHON, EMSCRIPTEN, filename + ('.o.ll' if append_ext else ''), '-o', filename + '.o.js'] + settings + extra_args, stdout=PIPE), None, 'Compiling') #print compiler_output # Detect compilation crashes and errors @@ -1041,7 +1070,7 @@ set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)''' % { 'winfix': '' if not WINDOWS e @staticmethod def js_optimizer(filename, passes, jcache): - return js_optimizer.run(filename, passes, NODE_JS, jcache) + return js_optimizer.run(filename, passes, listify(NODE_JS), jcache) @staticmethod def closure_compiler(filename): @@ -1086,24 +1115,6 @@ set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)''' % { 'winfix': '' if not WINDOWS e @staticmethod def is_bitcode(filename): - # checks if a file contains LLVM bitcode - # if the file doesn't exist or doesn't have valid symbols, it isn't bitcode - try: - defs = Building.llvm_nm(filename, stderr=PIPE) - # If no symbols found, it might just be an empty bitcode file, try to dis it - if len(defs.defs) + len(defs.undefs) + len(defs.commons) == 0: - # llvm-nm 3.0 has a bug when reading symbols from ar files - # so try to see if we're dealing with an ar file, in which - # case we should try to dis it. - if not Building.is_ar(filename): - test_ll = os.path.join(EMSCRIPTEN_TEMP_DIR, 'test.ll') - Building.llvm_dis(filename, test_ll) - assert os.path.exists(test_ll) - try_delete(test_ll) - except Exception, e: - if DEBUG: print >> sys.stderr, 'shared.Building.is_bitcode failed to test whether file \'%s\' is a llvm bitcode file! Failed on exception: %s' % (filename, e) - return False - # look for magic signature b = open(filename, 'r').read(4) if b[0] == 'B' and b[1] == 'C': @@ -1120,25 +1131,26 @@ set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)''' % { 'winfix': '' if not WINDOWS e # Make sure the relooper exists. If it does not, check out the relooper code and bootstrap it @staticmethod - def ensure_relooper(): - if os.path.exists(RELOOPER): return + def ensure_relooper(relooper): + if os.path.exists(relooper): return + Cache.ensure() curr = os.getcwd() try: ok = False print >> sys.stderr, '=======================================' print >> sys.stderr, 'bootstrapping relooper...' - Cache.ensure() os.chdir(path_from_root('src')) def make(opt_level): - raw = RELOOPER + '.raw.js' + raw = relooper + '.raw.js' Building.emcc(os.path.join('relooper', 'Relooper.cpp'), ['-I' + os.path.join('relooper'), '--post-js', os.path.join('relooper', 'emscripten', 'glue.js'), '-s', 'TOTAL_MEMORY=52428800', '-s', 'EXPORTED_FUNCTIONS=["_rl_set_output_buffer","_rl_make_output_buffer","_rl_new_block","_rl_delete_block","_rl_block_add_branch_to","_rl_new_relooper","_rl_delete_relooper","_rl_relooper_add_block","_rl_relooper_calculate","_rl_relooper_render", "_rl_set_asm_js_mode"]', '-s', 'DEFAULT_LIBRARY_FUNCS_TO_INCLUDE=["memcpy", "memset", "malloc", "free", "puts"]', + '-s', 'RELOOPER="' + relooper + '"', '-O' + str(opt_level), '--closure', '0'], raw) - f = open(RELOOPER, 'w') + f = open(relooper, 'w') f.write("// Relooper, (C) 2012 Alon Zakai, MIT license, https://github.com/kripken/Relooper\n") f.write("var Relooper = (function() {\n"); f.write(open(raw).read()) @@ -1158,180 +1170,39 @@ set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)''' % { 'winfix': '' if not WINDOWS e finally: os.chdir(curr) if not ok: - print >> sys.stderr, 'bootstrapping relooper failed. You may need to manually create src/relooper.js by compiling it, see src/relooper/emscripten' + print >> sys.stderr, 'bootstrapping relooper failed. You may need to manually create relooper.js by compiling it, see src/relooper/emscripten' 1/0 -# Permanent cache for dlmalloc and stdlibc++ -class Cache: - dirname = os.environ.get('EM_CACHE') - if not dirname: - dirname = os.path.expanduser(os.path.join('~', '.emscripten_cache')) - - @staticmethod - def ensure(): - if not os.path.exists(Cache.dirname): - os.makedirs(Cache.dirname) - - @staticmethod - def erase(): - try: - shutil.rmtree(Cache.dirname) - except: - pass - try_delete(RELOOPER) - - # Request a cached file. If it isn't in the cache, it will be created with - # the given creator function - @staticmethod - def get(shortname, creator): - if not shortname.endswith('.bc'): shortname += '.bc' - cachename = os.path.join(Cache.dirname, shortname) - if os.path.exists(cachename): - return cachename - Cache.ensure() - shutil.copyfile(creator(), cachename) - return cachename - -# JS-specific cache. We cache the results of compilation and optimization, -# so that in incremental builds we can just load from cache. -# We cache reasonably-large-sized chunks -class JCache: - dirname = os.path.join(Cache.dirname, 'jcache') - @staticmethod - def ensure(): - Cache.ensure() - if not os.path.exists(JCache.dirname): - os.makedirs(JCache.dirname) - - @staticmethod - def get_shortkey(keys): - if type(keys) not in [list, tuple]: - keys = [keys] - ret = '' - for key in keys: - assert type(key) == str - ret += hashlib.md5(key).hexdigest() - return ret - - @staticmethod - def get_cachename(shortkey): - return os.path.join(JCache.dirname, shortkey) + def preprocess(infile, outfile): + ''' + Preprocess source C/C++ in some special ways that emscripten needs. Returns + a filename (potentially the same one if nothing was changed). - # Returns a cached value, if it exists. Make sure the full key matches - @staticmethod - def get(shortkey, keys): - #if DEBUG: print >> sys.stderr, 'jcache get?', shortkey - cachename = JCache.get_cachename(shortkey) - if not os.path.exists(cachename): - #if DEBUG: print >> sys.stderr, 'jcache none at all' - return - data = cPickle.Unpickler(open(cachename, 'rb')).load() - if len(data) != 2: - #if DEBUG: print >> sys.stderr, 'jcache error in get' - return - oldkeys = data[0] - if len(oldkeys) != len(keys): - #if DEBUG: print >> sys.stderr, 'jcache collision (a)' - return - for i in range(len(oldkeys)): - if oldkeys[i] != keys[i]: - #if DEBUG: print >> sys.stderr, 'jcache collision (b)' - return - #if DEBUG: print >> sys.stderr, 'jcache win' - return data[1] - - # Sets the cached value for a key (from get_key) - @staticmethod - def set(shortkey, keys, value): - cachename = JCache.get_cachename(shortkey) - cPickle.Pickler(open(cachename, 'wb')).dump([keys, value]) - #if DEBUG: - # for i in range(len(keys)): - # open(cachename + '.key' + str(i), 'w').write(keys[i]) - # open(cachename + '.value', 'w').write(value) - - # Given a set of functions of form (ident, text), and a preferred chunk size, - # generates a set of chunks for parallel processing and caching. - # It is very important to generate similar chunks in incremental builds, in - # order to maximize the chance of cache hits. To achieve that, we save the - # chunking used in the previous compilation of this phase, and we try to - # generate the same chunks, barring big differences in function sizes that - # violate our chunk size guideline. If caching is not used, chunking_file - # should be None - @staticmethod - def chunkify(funcs, chunk_size, chunking_file): - previous_mapping = None - if chunking_file: - chunking_file = JCache.get_cachename(chunking_file) - if os.path.exists(chunking_file): - try: - previous_mapping = cPickle.Unpickler(open(chunking_file, 'rb')).load() # maps a function identifier to the chunk number it will be in - except: - pass - chunks = [] - if previous_mapping: - # initialize with previous chunking - news = [] - for func in funcs: - ident, data = func - if not ident in previous_mapping: - news.append(func) - else: - n = previous_mapping[ident] - while n >= len(chunks): chunks.append([]) - chunks[n].append(func) - # add news and adjust for new sizes - spilled = news - for chunk in chunks: - size = sum([len(func[1]) for func in chunk]) - while size > 1.5*chunk_size and len(chunk) > 0: - spill = chunk.pop() - spilled.append(spill) - size -= len(spill[1]) - for chunk in chunks: - size = sum([len(func[1]) for func in chunk]) - while size < 0.66*chunk_size and len(spilled) > 0: - spill = spilled.pop() - chunk.append(spill) - size += len(spill[1]) - chunks = filter(lambda chunk: len(chunk) > 0, chunks) # might have empty ones, eliminate them - funcs = spilled # we will allocate these into chunks as if they were normal inputs - # initialize reasonably, the rest of the funcs we need to split out - curr = [] - total_size = 0 - for i in range(len(funcs)): - func = funcs[i] - curr_size = len(func[1]) - if total_size + curr_size < chunk_size: - curr.append(func) - total_size += curr_size - else: - chunks.append(curr) - curr = [func] - total_size = curr_size - if curr: - chunks.append(curr) - curr = None - if chunking_file: - # sort within each chunk, to keep the order identical - for chunk in chunks: - chunk.sort(key=lambda func: func[0]) - # save new mapping info - new_mapping = {} - for i in range(len(chunks)): - chunk = chunks[i] - for ident, data in chunk: - new_mapping[ident] = i - cPickle.Pickler(open(chunking_file, 'wb')).dump(new_mapping) - #if DEBUG: - # if previous_mapping: - # for ident in set(previous_mapping.keys() + new_mapping.keys()): - # if previous_mapping.get(ident) != new_mapping.get(ident): - # print >> sys.stderr, 'mapping inconsistency', ident, previous_mapping.get(ident), new_mapping.get(ident) - # for key, value in new_mapping.iteritems(): - # print >> sys.stderr, 'mapping:', key, value - return [''.join([func[1] for func in chunk]) for chunk in chunks] # remove function names + Currently this only does emscripten_jcache_printf(..) rewriting. + ''' + src = open(infile).read() # stack warning on jcacheprintf! in docs # add jcache printf test separatrely, for content of printf + if 'emscripten_jcache_printf' not in src: return infile + def fix(m): + text = m.groups(0)[0] + assert text.count('(') == 1 and text.count(')') == 1, 'must have simple expressions in emscripten_jcache_printf calls, no parens' + assert text.count('"') == 2, 'must have simple expressions in emscripten_jcache_printf calls, no strings as varargs parameters' + start = text.index('(') + end = text.rindex(')') + args = text[start+1:end].split(',') + args = map(lambda x: x.strip(), args) + if args[0][0] == '"': + # flatten out + args = map(lambda x: str(ord(x)), args[0][1:len(args[0])-1]) + ['0'] + args[1:] + return 'emscripten_jcache_printf_(' + ','.join(args) + ')' + src = re.sub(r'(emscripten_jcache_printf\([^)]+\))', lambda m: fix(m), src) + open(outfile, 'w').write(src) + return outfile + +# compatibility with existing emcc, etc. scripts +Cache = cache.Cache(debug=DEBUG_CACHE) +JCache = cache.JCache(Cache) +chunkify = cache.chunkify class JS: @staticmethod diff --git a/tools/tempfiles.py b/tools/tempfiles.py new file mode 100644 index 00000000..1721b2bb --- /dev/null +++ b/tools/tempfiles.py @@ -0,0 +1,40 @@ +import os +import shutil +import tempfile + +def try_delete(filename): + try: + os.unlink(filename) + except: + if os.path.exists(filename): + shutil.rmtree(filename, ignore_errors=True) + +class TempFiles: + def __init__(self, tmp, save_debug_files=False): + self.tmp = tmp + self.save_debug_files = save_debug_files + + self.to_clean = [] + + def note(self, filename): + self.to_clean.append(filename) + + def get(self, suffix): + """Returns a named temp file with the given prefix.""" + named_file = tempfile.NamedTemporaryFile(dir=self.tmp, suffix=suffix, delete=False) + self.note(named_file.name) + return named_file + + def clean(self): + if self.save_debug_files: + print >> sys.stderr, 'not cleaning up temp files since in debug-save mode, see them in %s' % (self.tmp,) + return + for filename in self.to_clean: + try_delete(filename) + self.to_clean = [] + + def run_and_clean(self, func): + try: + return func() + finally: + self.clean() diff --git a/tools/test-js-optimizer-regs-output.js b/tools/test-js-optimizer-regs-output.js index 36006b7c..fe7de5fb 100644 --- a/tools/test-js-optimizer-regs-output.js +++ b/tools/test-js-optimizer-regs-output.js @@ -225,4 +225,8 @@ function switchey(r1) { r9 = r1 + 2; pp(r9); } +function __ZN14NetworkAddressC1EPKcti(r1) { + __ZN14NetworkAddressC2EPKcti(r1); + return; +} diff --git a/tools/test-js-optimizer-regs.js b/tools/test-js-optimizer-regs.js index 4802afa3..3013e518 100644 --- a/tools/test-js-optimizer-regs.js +++ b/tools/test-js-optimizer-regs.js @@ -230,4 +230,8 @@ function switchey(x) { var aaa = x+2; pp(aaa); } -// EMSCRIPTEN_GENERATED_FUNCTIONS: ["test", "primes", "atomic", "fcntl_open", "ex", "switchey"] +function __ZN14NetworkAddressC1EPKcti($this) { + __ZN14NetworkAddressC2EPKcti($this); + return; +} +// EMSCRIPTEN_GENERATED_FUNCTIONS: ["test", "primes", "atomic", "fcntl_open", "ex", "switchey", "__ZN14NetworkAddressC1EPKcti"] |