summaryrefslogtreecommitdiff
path: root/NativeLauncher/src/mono/mono_launcher.cc
blob: 8636447c1b74d60fe9d4c7eeb31c6d1ab7cf83c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248

#include "mono_launcher.h"
#include "utils.h"
#include "log.h"

#include <dlfcn.h>
#include <string>

namespace tizen {
namespace runtime {
namespace mono {

static const char* DOMAIN_NAME = "tizen_mono_domain";
static const char* LIBMONO = "/usr/lib/libmono-2.0.so.1";

MonoRuntime::MonoRuntime() :
  monolib(nullptr),
  domain(nullptr),
  launch(nullptr)
{

#define __XSTR(x) #x
#define __STR(x) __XSTR(x)

#ifdef MONO_LAUNCHER_ASSEMBLY_PATH
  launcherAssemblyPath = __STR(MONO_LAUNCHER_ASSEMBLY_PATH);
#endif

#ifdef DEVICE_API_DIR
  deviceAPIDirectory = __STR(DEVICE_API_DIR);
#endif
#ifdef RUNTIME_DIR
  runtimeDirectory = __STR(RUNTIME_DIR);
#endif

#undef __STR
#undef __XSTR
}

MonoRuntime::~MonoRuntime()
{
  Dispose();
}

int MonoRuntime::Initialize(bool standalone)
{
  if (standalone)
  {
    const char *_deviceapi_directory = getenv("DeviceAPIDirectory");
    const char *_runtime_directory = getenv("RuntimeDirectory");
    const char *_launcher_assembly = getenv("LauncherAssembly");
    if (_deviceapi_directory != nullptr)
      deviceAPIDirectory = _deviceapi_directory;
    if (_runtime_directory != nullptr)
      runtimeDirectory = _runtime_directory;
    if (_launcher_assembly != nullptr)
      launcherAssemblyPath = _launcher_assembly;
  }

  if (FileNotExist(LIBMONO))
  {
    _DBG("mono is not exist in %s", LIBMONO);
    return 1;
  }

  monolib = dlopen(LIBMONO, RTLD_LAZY);
#define MONOLIB_RETURN_IF_NOSYM(type, variable, name) \
  do { \
    variable = (type)dlsym(monolib, name); \
    if (variable == nullptr) { \
      _ERR(name " is not found in libmono"); \
      return 1; \
    }} while(0)

  MONOLIB_RETURN_IF_NOSYM(mono_set_dirs_ptr, SetDirs, "mono_set_dirs");
  MONOLIB_RETURN_IF_NOSYM(mono_set_assemblies_path_ptr, SetAssembliesPath, "mono_set_assemblies_path");
  MONOLIB_RETURN_IF_NOSYM(mono_jit_init_ptr, JitInit, "mono_jit_init");
  MONOLIB_RETURN_IF_NOSYM(mono_domain_assembly_open_ptr, DomainAssemblyOpen, "mono_domain_assembly_open");
  MONOLIB_RETURN_IF_NOSYM(mono_assembly_get_image_ptr, AssemblyGetImage, "mono_assembly_get_image");
  MONOLIB_RETURN_IF_NOSYM(mono_class_from_name_ptr, ClassFromName, "mono_class_from_name");
  MONOLIB_RETURN_IF_NOSYM(mono_runtime_invoke_ptr, RuntimeInvoke, "mono_runtime_invoke");
  MONOLIB_RETURN_IF_NOSYM(mono_class_get_method_from_name_ptr, ClassGetMethodFromName, "mono_class_get_method_from_name");
  MONOLIB_RETURN_IF_NOSYM(mono_object_to_string_ptr, ObjectToString, "mono_object_to_string");
  MONOLIB_RETURN_IF_NOSYM(mono_string_to_utf8_ptr, StringToUtf8, "mono_string_to_utf8");
  MONOLIB_RETURN_IF_NOSYM(mono_string_new_ptr, NewString, "mono_string_new");
  MONOLIB_RETURN_IF_NOSYM(mono_get_string_class_ptr, GetStringClass, "mono_get_string_class");
  MONOLIB_RETURN_IF_NOSYM(mono_array_new_ptr, ArrayNew, "mono_array_new");
  MONOLIB_RETURN_IF_NOSYM(mono_array_addr_with_size_ptr, ArrayAddrWithSize, "mono_array_addr_with_size");
  MONOLIB_RETURN_IF_NOSYM(mono_jit_cleanup_ptr, DomainCleanup, "mono_jit_cleanup");
  MONOLIB_RETURN_IF_NOSYM(mono_jit_exec_ptr, AssemblyExec, "mono_jit_exec");

#undef MONOLIB_RETURN_IF_NOSYM

  _DBG("libmono dlopen and dlsym success");

  return 0;
}

void MonoRuntime::Dispose()
{
  if (domain != nullptr)
  {
    DomainCleanup(domain);
  }
  if (monolib != nullptr && dlclose(monolib) != 0)
  {
    _ERR("libmono close failed");
  }
  monolib = nullptr;
}

int MonoRuntime::RunManagedLauncher()
{
  if (FileNotExist(launcherAssemblyPath.c_str()))
  {
    _ERR("Launcher Assembly is not exist in %s", launcherAssemblyPath.c_str());
    return 1;
  }

//  _DBG("mono_set_dirs(\"%s\", nullptr);", runtimeDirectory.c_str());
//  _DBG("mono_set_assemblies_path(\"%s\");", deviceAPIDirectory.c_str());

//  SetDirs(runtimeDirectory.c_str(), nullptr);
/*
  std::string assembliesPath = runtimeDirectory+":"+deviceAPIDirectory;
  _DBG("assembliesPath : %s", assembliesPath.c_str());
  SetAssembliesPath(assembliesPath.c_str());
  */
  SetAssembliesPath(deviceAPIDirectory.c_str());

  domain = JitInit(DOMAIN_NAME);
  if (domain == nullptr)
  {
    _ERR("Failed to init mono jit");
    return 1;
  }

  launcherAssembly = DomainAssemblyOpen(domain, launcherAssemblyPath.c_str());
  if (launcherAssembly == nullptr)
  {
    _ERR("Failed to Load Launcher Assembly");
    return 1;
  }

  monoImage = AssemblyGetImage(launcherAssembly);
  if (monoImage == nullptr)
  {
    _ERR("Failed to get image from launcher assembly");
    return 1;
  }

  assemblyManagerClass = ClassFromName(monoImage, "Tizen.Runtime.Mono", "AssemblyManager");
  if (assemblyManagerClass == nullptr)
  {
    _ERR("Failed to get AssemblyManager class in namespace Tizen.Runtime.Mono from launcher image");
    return 1;
  }

  prepareLaunch = ClassGetMethodFromName(assemblyManagerClass, "Prepared", 0);
  if (prepareLaunch == nullptr)
  {
    _ERR("Failed to get Prepared() method from Tizen.Runtime.Mono.AssemblyManager");
    return 1;
  }
  MonoObject* exception = nullptr;
  RuntimeInvoke(prepareLaunch, nullptr, nullptr, &exception);
  if (exception != nullptr)
  {
    MonoString * exceptionMsg = ObjectToString(exception, nullptr);
    char* cstringMsg = StringToUtf8(exceptionMsg);
    _ERR("Failed to invoke method in runtime");
    _ERR("%s", cstringMsg);
    free(cstringMsg);
    return 1;
  }

  launch = ClassGetMethodFromName(assemblyManagerClass, "Launch", 4);

  return 0;
}

#define ArrayAddr(array,type,index) ((type*)(void*) ArrayAddrWithSize (array, sizeof (type), index))
#define ArraySet(array,type,index,value)	\
	do {	\
		type *__p = (type *) ArrayAddr ((array), type, (index));	\
		*__p = (value);	\
	} while (0)

int MonoRuntime::Launch(const char* root, const char* path, int argc, char* argv[])
{
  if (domain == nullptr)
  {
    domain = JitInit(DOMAIN_NAME);
    if (domain == nullptr)
    {
      _ERR("Failed to init mono jit");
      return 1;
    }
  }
  if (launch != nullptr)
  {
    MonoString *rootMonoString = NewString(domain, root);
    MonoString *pathMonoString = NewString(domain, path);
    MonoArray *argvMonoArray = ArrayNew(domain, GetStringClass(), argc);
    for (int i=0; i<argc; i++)
    {
      MonoString *arg = NewString(domain, argv[i]);
      ArraySet(argvMonoArray, MonoString*, i, arg);
    }

    void **args = new void*[argc+2];
    args[0] = rootMonoString;
    args[1] = pathMonoString;
    args[2] = &argc;
    args[3] = argvMonoArray;

    MonoObject* exception = nullptr;
    RuntimeInvoke(launch, nullptr, args, &exception);
    if (exception != nullptr)
    {
      MonoString * exceptionMsg = ObjectToString(exception, nullptr);
      char* cstringMsg = StringToUtf8(exceptionMsg);
      _ERR("Failed to invoke launch method in runtime");
      _ERR("%s", cstringMsg);
      free(cstringMsg);
      return 1;
    }
  }
  else
  {
    std::string appBin = ConcatPath(root, "bin");
    std::string appLib = ConcatPath(root, "lib");
    std::string probePath = deviceAPIDirectory + ":" + appBin + ":" + appLib;
    SetAssembliesPath(probePath.c_str());
    MonoAssembly* app = DomainAssemblyOpen(domain, path);
    if (app == nullptr)
    {
      _ERR("Failed to open assembly : %s", path);
      return 1;
    }
    int ret = AssemblyExec(domain, app, argc, argv);
  }
  return 0;
}

}  // namespace dotnetcore
}  // namespace runtime
}  // namespace mono