• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2#
3# Copyright 2013 the V8 project authors. All rights reserved.
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met:
7#
8#     * Redistributions of source code must retain the above copyright
9#       notice, this list of conditions and the following disclaimer.
10#     * Redistributions in binary form must reproduce the above
11#       copyright notice, this list of conditions and the following
12#       disclaimer in the documentation and/or other materials provided
13#       with the distribution.
14#     * Neither the name of Google Inc. nor the names of its
15#       contributors may be used to endorse or promote products derived
16#       from this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30# This script executes the passed command line using the Native Client
31# 'sel_ldr' container. It is derived from android-run.py.
32
33import os
34from os.path import join, dirname, abspath
35import re
36import subprocess
37import sys
38import tempfile
39
40def Check(output, errors):
41  failed = any([s.startswith('/system/bin/sh:') or s.startswith('ANDROID')
42                for s in output.split('\n')])
43  return 1 if failed else 0
44
45def Execute(cmdline):
46  (fd_out, outname) = tempfile.mkstemp()
47  (fd_err, errname) = tempfile.mkstemp()
48  process = subprocess.Popen(
49    args=cmdline,
50    shell=True,
51    stdout=fd_out,
52    stderr=fd_err,
53  )
54  exit_code = process.wait()
55  os.close(fd_out)
56  os.close(fd_err)
57  output = file(outname).read()
58  errors = file(errname).read()
59  os.unlink(outname)
60  os.unlink(errname)
61  sys.stdout.write(output)
62  sys.stderr.write(errors)
63  return exit_code or Check(output, errors)
64
65def Escape(arg):
66  def ShouldEscape():
67    for x in arg:
68      if not x.isalnum() and x != '-' and x != '_':
69        return True
70    return False
71
72  return arg if not ShouldEscape() else '"%s"' % (arg.replace('"', '\\"'))
73
74def WriteToTemporaryFile(data):
75  (fd, fname) = tempfile.mkstemp()
76  os.close(fd)
77  tmp_file = open(fname, "w")
78  tmp_file.write(data)
79  tmp_file.close()
80  return fname
81
82def GetNaClArchFromNexe(nexe):
83  try:
84    p = subprocess.Popen(['file', nexe], stdout=subprocess.PIPE)
85    out, err = p.communicate()
86    lines = [re.sub("\s+", " " , line) for line in out.split('\n')]
87    if lines[0].find(": ELF 32-bit LSB executable, Intel 80386") > 0:
88      return "x86_32"
89    if lines[0].find(": ELF 64-bit LSB executable, x86-64") > 0:
90      return "x86_64"
91  except:
92    print 'file ' + sys.argv[1] + ' failed'
93  return None
94
95def GetNaClResources(nexe):
96  nacl_sdk_dir = os.environ["NACL_SDK_ROOT"]
97  nacl_arch = GetNaClArchFromNexe(nexe)
98  if sys.platform.startswith("linux"):
99    platform = "linux"
100  elif sys.platform == "darwin":
101    platform = "mac"
102  else:
103    print("NaCl V8 testing is supported on Linux and MacOS only.")
104    sys.exit(1)
105
106  if nacl_arch is "x86_64":
107    toolchain = platform + "_x86_glibc"
108    sel_ldr = "sel_ldr_x86_64"
109    irt = "irt_core_x86_64.nexe"
110    libdir = "lib64"
111  elif nacl_arch is "x86_32":
112    toolchain = platform + "_x86_glibc"
113    sel_ldr = "sel_ldr_x86_32"
114    irt = "irt_core_x86_32.nexe"
115    libdir = "lib32"
116  elif nacl_arch is "arm":
117    print("NaCl V8 ARM support is not ready yet.")
118    sys.exit(1)
119  else:
120    print("Invalid nexe %s with NaCl arch %s" % (nexe, nacl_arch))
121    sys.exit(1)
122
123  nacl_sel_ldr = os.path.join(nacl_sdk_dir, "tools", sel_ldr)
124  nacl_irt = os.path.join(nacl_sdk_dir, "tools", irt)
125
126  return (nacl_sdk_dir, nacl_sel_ldr, nacl_irt)
127
128def Main():
129  if (len(sys.argv) == 1):
130    print("Usage: %s <command-to-run-on-device>" % sys.argv[0])
131    return 1
132
133  args = [Escape(arg) for arg in sys.argv[1:]]
134
135  (nacl_sdk_dir, nacl_sel_ldr, nacl_irt) = GetNaClResources(sys.argv[1])
136
137  # sel_ldr Options:
138  # -c -c: disable validation (for performance)
139  # -a: allow file access
140  # -B <irt>: load the IRT
141  command = ' '.join([nacl_sel_ldr, '-c', '-c', '-a', '-B', nacl_irt, '--'] +
142                     args)
143  error_code = Execute(command)
144  return error_code
145
146if __name__ == '__main__':
147  sys.exit(Main())
148