local_test.cpp revision 7c04242d6fcd486d16c7848fed599cbda658f6d7
17c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris/*
27c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris * Copyright (C) 2014 The Android Open Source Project
37c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris *
47c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris * Licensed under the Apache License, Version 2.0 (the "License");
57c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris * you may not use this file except in compliance with the License.
67c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris * You may obtain a copy of the License at
77c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris *
87c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris *      http://www.apache.org/licenses/LICENSE-2.0
97c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris *
107c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris * Unless required by applicable law or agreed to in writing, software
117c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris * distributed under the License is distributed on an "AS IS" BASIS,
127c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris * See the License for the specific language governing permissions and
147c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris * limitations under the License.
157c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris */
167c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris
177c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris#include <stdint.h>
187c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris
197c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris#include <gtest/gtest.h>
207c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris
217c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris#define UNW_LOCAL_ONLY
227c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris#include <libunwind.h>
237c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris
247c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris#define EXTRA_CONTEXT_BYTES 1024
257c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris
267c04242d6fcd486d16c7848fed599cbda658f6d7Christopher FerrisTEST(libbacktrace, getcontext_size) {
277c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris  unw_context_t* context;
287c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris  context = reinterpret_cast<unw_context_t*>(malloc(sizeof(unw_context_t) + EXTRA_CONTEXT_BYTES));
297c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris  ASSERT_TRUE(context != NULL);
307c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris  uint8_t* extra = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(context) + sizeof(unw_context_t));
317c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris  for (size_t i = 0; i < EXTRA_CONTEXT_BYTES; i++) {
327c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris    extra[i] = (i % 255) + 1;
337c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris  }
347c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris  ASSERT_TRUE(unw_getcontext(context) == 0);
357c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris  /* Check that nothing was written past the end of the structure. */
367c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris  for (size_t i = 0; i < EXTRA_CONTEXT_BYTES; i++) {
377c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris    ASSERT_EQ((i % 255) + 1, extra[i]);
387c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris  }
397c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris
407c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris  free(context);
417c04242d6fcd486d16c7848fed599cbda658f6d7Christopher Ferris}
42