1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 */ 17 #include <jni.h> 18 #include <inttypes.h> 19 #include <setjmp.h> 20 #include <signal.h> 21 #include <stdbool.h> 22 #include <stdlib.h> 23 24 //mask the top 8 bits 25 #define TAG_MASK ((0xFFULL) << 56) 26 27 #define PATTERN 0x600DC0DE 28 29 static sigjmp_buf jmpenv; 30 31 static void sigsegv_handler(int signum) { 32 siglongjmp(jmpenv, 1); 33 } 34 35 jboolean android_os_cts_TaggedPointer_hasTaggedPointer(JNIEnv* env, jobject thiz) 36 { 37 uint32_t data; 38 uint32_t *tagged; 39 uintptr_t tmp; 40 int err; 41 jboolean ret = true; 42 struct sigaction sigsegv_act; 43 struct sigaction oldact; 44 45 tmp = TAG_MASK | (uintptr_t)(&data); 46 tagged = (uint32_t *)tmp; 47 data = PATTERN; 48 49 memset(&sigsegv_act, 0, sizeof(sigsegv_act)); 50 sigsegv_act.sa_handler = sigsegv_handler; 51 52 err = sigaction(SIGSEGV, &sigsegv_act, &oldact); 53 if (err) { 54 ret = false; 55 goto err_sigaction; 56 } 57 58 if (sigsetjmp(jmpenv, 1)) { 59 ret = false; 60 goto err_segfault; 61 } 62 63 if (*tagged != PATTERN) { 64 ret = false; 65 } 66 67 err_segfault: 68 sigaction(SIGSEGV, &oldact, NULL); 69 err_sigaction: 70 return ret; 71 } 72 73 static JNINativeMethod gMethods[] = { 74 { "hasTaggedPointer", "()Z", 75 (void *) android_os_cts_TaggedPointer_hasTaggedPointer }, 76 }; 77 78 int register_android_os_cts_TaggedPointer(JNIEnv* env) 79 { 80 jclass clazz = env->FindClass("android/os/cts/TaggedPointer"); 81 82 return env->RegisterNatives(clazz, gMethods, 83 sizeof(gMethods) / sizeof(JNINativeMethod)); 84 } 85